public
Rubygem
Description: Johnson wraps JavaScript in a loving Ruby embrace.
Homepage: http://github.com/jbarnette/johnson/wikis
Clone URL: git://github.com/jbarnette/johnson.git
Search Repo:
aaronp (author)
Fri May 09 10:28:57 -0700 2008
commit  2fc2742302331b2a95a339c5c41977e08ce1ce66
tree    07e04c7b1ce4664c5bea8c5bfde7cf678aaffc65
parent  17c439562837ef5473f86a3ebadc628a1d6cbc0e
johnson / ext / spidermonkey / immutable_node.c.erb
100644 518 lines (448 sloc) 12.903 kb
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "immutable_node.h"
 
static VALUE cNode;
 
static void deallocate(ImmutableNodeContext* context)
{
  if (context->pc)
  {
    js_FinishParseContext(context->js, context->pc);
    free(context->pc);
  }
 
  JS_DestroyContext(context->js);
  JS_DestroyRuntime(context->runtime);
  free(context);
}
 
static VALUE allocate(VALUE klass)
{
  ImmutableNodeContext * context = calloc(1, sizeof(ImmutableNodeContext));
 
  VALUE self = Data_Wrap_Struct(klass, 0, deallocate, context);
 
  assert(context->runtime = JS_NewRuntime(0x100000));
  assert(context->js = JS_NewContext(context->runtime, 8192));
 
  return self;
}
 
/*
 * call-seq:
 * parse_io(stream, filename=nil, line_number=0)
 *
 * Parses an IO object, returning a native spidermonkey parse tree.
 */
static VALUE parse_io(int argc, VALUE *argv, VALUE klass) {
  VALUE self = allocate(klass);
  VALUE stream, filename, linenum;
 
  ImmutableNodeContext* context;
  Data_Get_Struct(self, ImmutableNodeContext, context);
 
  assert(context->pc = calloc(1, sizeof(JSParseContext)));
 
  rb_scan_args( argc, argv, "12", &stream, &filename, &linenum );
  
  VALUE file_contents = rb_funcall(stream, rb_intern("read"), 0);
  size_t length = NUM2INT(rb_funcall(file_contents, rb_intern("length"), 0));
 
  jschar* chars;
  assert(chars = js_InflateString(context->js, StringValuePtr(file_contents), &length));
 
  if(!filename && rb_respond_to(stream, rb_intern("path"))) {
    filename = rb_funcall(stream, rb_intern("path"), 0);
  }
  char* filenamez = RTEST(filename) ? StringValueCStr(filename) : NULL;
  int linenumi = RTEST(linenum) ? NUM2INT(linenum) : 1;
 
  assert(js_InitParseContext(context->js, context->pc,
      NULL,
      chars,
      length,
      NULL, filenamez, linenumi));
 
  context->node = js_ParseScript(context->js,
      JS_NewObject(context->js, NULL, NULL, NULL),
      context->pc);
  if(JS_IsExceptionPending(context->js)) {
    jsval exception, message, file_name, line_number;
    JS_GetPendingException(context->js, &exception);
    JS_GetProperty(context->js, JSVAL_TO_OBJECT(exception), "message",&message);
    JS_GetProperty(context->js, JSVAL_TO_OBJECT(exception), "fileName",&file_name);
    JS_GetProperty(context->js, JSVAL_TO_OBJECT(exception), "lineNumber",&line_number);
    JS_ClearPendingException(context->js);
 
    rb_funcall( self,
                rb_intern("raise_parse_error"),
                3,
                message == JSVAL_NULL ?
                  Qnil :
                  rb_str_new2(JS_GetStringBytes(JSVAL_TO_STRING(message))),
                rb_str_new2(JS_GetStringBytes(JSVAL_TO_STRING(file_name))),
                INT2NUM(JSVAL_TO_INT(line_number))
                );
 
  }
  return self;
}
 
/*
 * call-seq:
 * line
 *
 * Returns the line number of the node.
 */
static VALUE line(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  return INT2NUM(ctx->node->pn_pos.begin.lineno);
}
 
/*
 * call-seq:
 * index
 *
 * Returns the column number of the node.
 */
static VALUE begin_index(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  return INT2NUM(ctx->node->pn_pos.begin.index);
}
 
/*
 * call-seq:
 * pn_arity
 *
 * Returns the arity of the node as a symbol.
 */
static VALUE pn_arity(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  switch(ctx->node->pn_arity) {
    case PN_FUNC:
      return ID2SYM(rb_intern("pn_func"));
    case PN_LIST:
      return ID2SYM(rb_intern("pn_list"));
    case PN_TERNARY:
      return ID2SYM(rb_intern("pn_ternary"));
    case PN_BINARY:
      return ID2SYM(rb_intern("pn_binary"));
    case PN_UNARY:
      return ID2SYM(rb_intern("pn_unary"));
    case PN_NAME:
      return ID2SYM(rb_intern("pn_name"));
    case PN_NULLARY:
      return ID2SYM(rb_intern("pn_nullary"));
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_type
 *
 * Returns the type of the node as a symbol.
 */
static VALUE pn_type(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  switch(ctx->node->pn_type) {
    <% tokens.each do |token| %>
    case <%= token %>: return ID2SYM(rb_intern("<%= token.downcase %>"));
    <% end %>
  }
  return INT2NUM(ctx->node->pn_type);
}
 
/*
 * call-seq:
 * pn_expr
 *
 * Returns the parse node expression as an ImmutableNode.
 */
static VALUE data_pn_expr(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  if(ctx->node->pn_expr) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_expr;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_kid
 *
 * Returns the child ImmutableNode.
 */
static VALUE data_pn_kid(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  if(ctx->node->pn_kid) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_kid;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_kid1
 *
 * Returns the first child ImmutableNode.
 */
static VALUE data_pn_kid1(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  if(ctx->node->pn_kid1) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_kid1;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_kid2
 *
 * Returns the second child ImmutableNode.
 */
static VALUE data_pn_kid2(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  if(ctx->node->pn_kid2) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_kid2;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_kid3
 *
 * Returns the third child ImmutableNode.
 */
static VALUE data_pn_kid3(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  if(ctx->node->pn_kid3) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_kid3;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_dval
 *
 * Returns the numeric value of the node.
 */
static VALUE data_pn_dval(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  if(JSVAL_IS_NUMBER(ATOM_KEY(ctx->node->pn_atom))) {
    return rb_float_new(ctx->node->pn_dval);
  } else {
    return INT2NUM((int)(ctx->node->pn_dval));
  }
}
 
/*
 * call-seq:
 * pn_op
 *
 * Returns the op code for the node as a symbol.
 */
static VALUE data_pn_op(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  switch(ctx->node->pn_op) {
    <% jsops.each do |jsop| %>
    case <%= jsop %>: return ID2SYM(rb_intern("<%= jsop.downcase %>"));
    <% end %>
  }
  return INT2NUM(ctx->node->pn_op);
}
 
/*
 * call-seq:
 * pn_left
 *
 * Returns the left side ImmutableNode.
 */
static VALUE data_pn_left(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
 
  if(ctx->node->pn_left) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_left;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_extra
 *
 * Returns extra informaton about the node as an Integer.
 */
static VALUE data_pn_extra(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
 
  return INT2NUM(ctx->node->pn_extra);
}
 
/*
 * call-seq:
 * name
 *
 * Returns the name of the node.
 */
static VALUE name(VALUE self) {
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  return rb_str_new2(JS_GetStringBytes(ATOM_TO_STRING(ctx->node->pn_atom)));
}
 
/*
 * call-seq:
 * regexp
 *
 * Returns the regexp value as a String.
 */
static VALUE regexp(VALUE self) {
  ImmutableNodeContext * ctx;
  jsval result;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  js_regexp_toString(ctx->js, ctx->node->pn_pob->object, &result);
  return rb_str_new2(JS_GetStringBytes(JSVAL_TO_STRING(result)));
}
 
/*
 * call-seq:
 * function_name
 *
 * Returns the function name as a String.
 */
static VALUE function_name(VALUE self) {
  ImmutableNodeContext * ctx;
  JSFunction * f;
  JSObject * object;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  object = ctx->node->pn_funpob->object;
  f = (JSFunction *)JS_GetPrivate(ctx->js, ctx->node->pn_funpob->object);
 
  if(f->atom) {
    return rb_str_new2(JS_GetStringBytes(ATOM_TO_STRING(f->atom)));
  } else {
    return Qnil;
  }
}
 
/*
 * call-seq:
 * function_args
 *
 * Returns the function argument names as an Array of String.
 */
static VALUE function_args(VALUE self) {
  ImmutableNodeContext * ctx;
  JSFunction * f;
  JSObject * object;
  jsuword* names;
  VALUE func_args;
  int i;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  object = ctx->node->pn_funpob->object;
  f = (JSFunction *)JS_GetPrivate(ctx->js, ctx->node->pn_funpob->object);
 
  func_args = rb_ary_new2(f->nargs);
  if(f->nargs > 0) {
    names = js_GetLocalNameArray(ctx->js, f, &ctx->js->tempPool);
    for(i = 0; i < f->nargs; i++) {
      rb_ary_push(func_args,
          rb_str_new2(JS_GetStringBytes(ATOM_TO_STRING(names[i])))
          );
    }
  }
  return func_args;
}
 
/*
 * call-seq:
 * function_body
 *
 * Returns the function body as an ImmutableNode.
 */
static VALUE function_body(VALUE self) {
  ImmutableNodeContext * ctx;
  JSFunction * f;
  JSObject * object;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  object = ctx->node->pn_funpob->object;
  f = (JSFunction *)JS_GetPrivate(ctx->js, ctx->node->pn_funpob->object);
 
  if(ctx->node->pn_body) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_body;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * pn_right
 *
 * Returns right side as an ImmutableNode.
 */
static VALUE data_pn_right(VALUE self)
{
  ImmutableNodeContext * ctx;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
 
  if(ctx->node->pn_right) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = ctx->node->pn_right;
    return node;
  }
  return Qnil;
}
 
/*
 * call-seq:
 * children
 *
 * Returns children as an Array of ImmutableNode.
 */
static VALUE children(VALUE self) {
  ImmutableNodeContext * ctx;
  JSParseNode * p;
  VALUE children;
 
  Data_Get_Struct(self, ImmutableNodeContext, ctx);
  children = rb_ary_new();
  for(p = ctx->node->pn_head; p != NULL; p = p->pn_next) {
    ImmutableNodeContext *roc;
    VALUE node = Data_Make_Struct(cNode, ImmutableNodeContext, NULL, NULL, roc);
    roc->js = ctx->js;
    roc->node = p;
 
    rb_ary_push(children, node);
  }
 
  return children;
}
 
void init_Johnson_SpiderMonkey_Immutable_Node(VALUE spidermonkey)
{
  /* HACK: These comments are *only* to make RDoc happy.
  VALUE johnson = rb_define_module("Johnson");
  VALUE spidermonkey = rb_define_module_under(johnson, "SpiderMonkey");
  */
 
  /* ImmutableNode class. */
  cNode = rb_define_class_under(spidermonkey, "ImmutableNode", rb_cObject);
 
  rb_define_alloc_func(cNode, allocate);
  rb_define_singleton_method(cNode, "parse_io", parse_io, -1);
  rb_define_method(cNode, "line", line, 0);
  rb_define_method(cNode, "index", begin_index, 0);
  rb_define_method(cNode, "pn_arity", pn_arity, 0);
  rb_define_method(cNode, "pn_type", pn_type, 0);
  rb_define_method(cNode, "pn_expr", data_pn_expr, 0);
  rb_define_method(cNode, "pn_kid", data_pn_kid, 0);
  rb_define_method(cNode, "pn_kid1", data_pn_kid1, 0);
  rb_define_method(cNode, "pn_kid2", data_pn_kid2, 0);
  rb_define_method(cNode, "pn_kid3", data_pn_kid3, 0);
  rb_define_method(cNode, "pn_dval", data_pn_dval, 0);
  rb_define_method(cNode, "pn_op", data_pn_op, 0);
  rb_define_method(cNode, "pn_left", data_pn_left, 0);
  rb_define_method(cNode, "pn_extra", data_pn_extra, 0);
  rb_define_method(cNode, "name", name, 0);
  rb_define_method(cNode, "regexp", regexp, 0);
  rb_define_method(cNode, "function_name", function_name, 0);
  rb_define_method(cNode, "function_args", function_args, 0);
  rb_define_method(cNode, "function_body", function_body, 0);
  rb_define_method(cNode, "pn_right", data_pn_right, 0);
  rb_define_method(cNode, "children", children, 0);
}