File: simple.c

package info (click to toggle)
ngs-js 0.2.5-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,800 kB
  • ctags: 2,977
  • sloc: ansic: 34,455; sh: 4,827; perl: 261; makefile: 208
file content (338 lines) | stat: -rw-r--r-- 7,639 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * An example how to embed the JavaScript interpreter to your program.
 * Copyright (c) 1998-1999 New Generation Software (NGS) Oy
 *
 * Author: Markku Rossi <mtr@ngs.fi>
 */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

/*
 * $Source: /usr/local/cvsroot/ngs/js/examples/simple.c,v $
 * $Id: simple.c,v 1.7 1999/01/15 09:01:44 mtr Exp $
 */

#include <stdio.h>
#include <stdlib.h>

#include <js.h>

/*
 * Static functions.
 */

/* I/O function for the standard error stream. */
int
io_stderr (void *context, unsigned char *buffer, unsigned int amount)
{
  return fwrite (buffer, 1, amount, stderr);
}


/* The `hello' command. */
JSMethodResult
hello_proc (void *context, JSInterpPtr interp, int argc, JSType *argv,
	    JSType *result_return, char *error_return)
{
  char *msg = context;

  printf ("%s\n", msg);

  return JS_OK;
}

/* The `add' command. */
JSMethodResult
add_proc (void *context, JSInterpPtr interp, int argc, JSType *argv,
	  JSType *result_return, char *error_return)
{
  int i;
  double d = 0.0;
  int doubles = 0;

  for (i = 0; i < argc; i++)
    {
      JSType *arg = &argv[i];

      switch (arg->type)
	{
	case JS_TYPE_INTEGER:
	  d += (double) arg->u.i;
	  break;

	case JS_TYPE_DOUBLE:
	  d += arg->u.d;
	  doubles++;
	  break;

	default:
	  sprintf (error_return, "add: illegal argument");
	  return JS_ERROR;
	  break;
	}
    }

  if (doubles == 0)
    {
      result_return->type = JS_TYPE_INTEGER;
      result_return->u.i = (long) d;
    }
  else
    {
      result_return->type = JS_TYPE_DOUBLE;
      result_return->u.d = d;
    }

  return JS_OK;
}

/* Methods for the `Hello' class. */

/* The instance context for Hello objects. */
struct hello_ctx_st
{
  char msg[1024];
};

typedef struct hello_ctx_st HelloCtx;

/* Method `Hello.show()'. */
static JSMethodResult
hello_show (JSClassPtr cls, void *instance_context, JSInterpPtr interp,
	    int argc, JSType *argv, JSType *result_return, char *error_return)
{
  HelloCtx *ictx = instance_context;
  char *msg;

  if (ictx)
    msg = ictx->msg;
  else
    msg = js_class_context (cls);

  printf ("%s\n", msg);

  return JS_OK;
}

/* Method `Hello.fail()'. */
static JSMethodResult
hello_fail (JSClassPtr cls, void *instance_context, JSInterpPtr interp,
	    int argc, JSType *argv, JSType *result_return, char *error_return)
{
  strcpy (error_return, "My mission is to fail!");

  return JS_ERROR;
}

/* Method `Hello.investigate(obj)'. */
static JSMethodResult
hello_investigate (JSClassPtr cls, void *instance_context, JSInterpPtr interp,
		   int argc, JSType *argv, JSType *result_return,
		   char *error_return)
{
  HelloCtx *others_ictx;

  if (argc != 1)
    {
      strcpy (error_return, "illegal amount of arguments");
      return JS_ERROR;
    }
  if (!js_isa (interp, &argv[0], js_lookup_class (interp, "Hello"),
	       (void **) &others_ictx))
    {
      strcpy (error_return, "illegal argument");
      return JS_ERROR;
    }

  printf ("Hello.investigate(): my argument's message is \"%s\"\n",
	  others_ictx->msg);

  return JS_OK;
}

/* Take a copy of us. */
static JSMethodResult
hello_copy (JSClassPtr cls, void *instance_context, JSInterpPtr interp,
	    int argc, JSType *argv, JSType *result_return,
	    char *error_return)
{
  HelloCtx *ictx = instance_context;
  HelloCtx *nictx;
  JSClassPtr rcls;

  rcls = js_lookup_class (interp, "Hello");
  if (cls != rcls)
    {
      sprintf (error_return, "internal class error");
      return JS_ERROR;
    }

  nictx = malloc (sizeof (*nictx));
  if (ictx)
    strcpy (nictx->msg, ictx->msg);
  else
    strcpy (nictx->msg, "Hello, world!!!!!");

  js_instantiate_class (interp, cls, nictx, free, result_return);

  return JS_OK;
}

/* The getter function for the `Hello.msg' property. */
static JSMethodResult
hello_msg (JSClassPtr cls, void *instance_context, JSInterpPtr interp,
	   int setp, JSType *value, char *error_return)
{
  HelloCtx *ictx = instance_context;
  char *msg;

  if (ictx)
    msg = ictx->msg;
  else
    msg = js_class_context (cls);

  js_type_make_string (interp, value, msg, strlen (msg));
}


static JSMethodResult
hello_constructor (JSClassPtr cls, JSInterpPtr interp, int argc, JSType *argv,
		   void **ictx_return, JSFreeProc *ictx_destructor_return,
		   char *error_return)
{
  HelloCtx *ictx;
  int len;

  if (argc != 1)
    {
      strcpy (error_return, "wront amount of arguments");
      return JS_ERROR;
    }
  if (argv[0].type != JS_TYPE_STRING)
    {
      strcpy (error_return, "illegal argument");
      return JS_ERROR;
    }

  ictx = calloc (1, sizeof (*ictx));
  if (ictx == NULL)
    {
      strcpy (error_return, "out of memory");
      return JS_ERROR;
    }

  len = argv[0].u.s->len;
  if (len + 1 > sizeof (ictx->msg))
    len = sizeof (ictx->msg) - 1;

  memcpy (ictx->msg, argv[0].u.s->data, len);
  ictx->msg[len] = '\0';

  *ictx_return = ictx;
  *ictx_destructor_return = free;

  return JS_OK;
}


/*
 * Global functions.
 */

int
main (int argc, char *argv[])
{
  JSInterpPtr interp;
  JSInterpOptions options;
  JSClassPtr cls;

  int i;

  if (argc < 2)
    {
      fprintf (stderr, "Usage: %s FILE...\n", argv[0]);
      exit (1);
    }

  /* Create one interpreter. */

  js_init_default_options (&options);

  options.verbose = 2;
  options.s_stderr = io_stderr;
  options.s_context = NULL;

  interp = js_create_interp (&options);

  /* Create our extensions. */
  js_create_global_method (interp, "hello", hello_proc,
			   "(global method) Hello, world!", NULL);
  js_create_global_method (interp, "add", add_proc, NULL, NULL);

  /* Create our class. */
  cls = js_class_create ("(Hello) Hello, world!", NULL, 0,
			 hello_constructor);

  js_class_define_method (cls, "show", JS_CF_STATIC, hello_show);
  js_class_define_method (cls, "fail", JS_CF_STATIC, hello_fail);
  js_class_define_method (cls, "investigate", JS_CF_STATIC, hello_investigate);
  js_class_define_method (cls, "copy", 0, hello_copy);

  js_class_define_property (cls, "msg", JS_CF_STATIC | JS_CF_IMMUTABLE,
			    hello_msg);

  js_define_class (interp, cls, "Hello");

  /* Evaluate all argument files. */
  for (i = 1; i < argc; i++)
    if (!js_eval_file (interp, argv[i]))
      fprintf (stderr, "evaluation of file `%s' failed: %s\n",
	       argv[i], js_error_message (interp));

  /* Call our add command with the js_apply() function. */
  {
    JSType argv[3];

    argv[0].type = JS_TYPE_INTEGER;
    argv[0].u.i = 42;

    argv[1].type = JS_TYPE_INTEGER;
    argv[1].u.i = 72;

    if (!js_apply (interp, "add", 2, argv))
      {
	fprintf (stderr, "js_apply() failed: %s\n", js_error_message (interp));
	exit (1);
      }

    js_result (interp, &argv[2]);

    if (argv[2].type != JS_TYPE_INTEGER)
      {
	fprintf (stderr, "add() returned wrong type!\n");
	exit (1);
      }

    printf ("%ld + %ld = %ld\n", argv[0].u.i, argv[1].u.i, argv[2].u.i);
  }

  /* Cleanup. */
  js_destroy_interp (interp);

  return 1;
}