jbarnette-johnson 1.0.0.200806240111

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. data/CHANGELOG +5 -0
  2. data/MANIFEST +385 -0
  3. data/MINGW32.mk +124 -0
  4. data/README.rdoc +51 -0
  5. data/Rakefile +166 -0
  6. data/bin/johnson +107 -0
  7. data/cross-compile.txt +38 -0
  8. data/ext/spidermonkey/context.c +122 -0
  9. data/ext/spidermonkey/context.h +19 -0
  10. data/ext/spidermonkey/conversions.c +286 -0
  11. data/ext/spidermonkey/conversions.h +18 -0
  12. data/ext/spidermonkey/debugger.c +208 -0
  13. data/ext/spidermonkey/debugger.h +9 -0
  14. data/ext/spidermonkey/extconf.rb +25 -0
  15. data/ext/spidermonkey/extensions.c +37 -0
  16. data/ext/spidermonkey/extensions.h +12 -0
  17. data/ext/spidermonkey/global.c +40 -0
  18. data/ext/spidermonkey/global.h +11 -0
  19. data/ext/spidermonkey/idhash.c +16 -0
  20. data/ext/spidermonkey/idhash.h +8 -0
  21. data/ext/spidermonkey/immutable_node.c.erb +522 -0
  22. data/ext/spidermonkey/immutable_node.h +22 -0
  23. data/ext/spidermonkey/jroot.h +187 -0
  24. data/ext/spidermonkey/js_land_proxy.c +609 -0
  25. data/ext/spidermonkey/js_land_proxy.h +20 -0
  26. data/ext/spidermonkey/ruby_land_proxy.c +537 -0
  27. data/ext/spidermonkey/ruby_land_proxy.h +17 -0
  28. data/ext/spidermonkey/runtime.c +304 -0
  29. data/ext/spidermonkey/runtime.h +25 -0
  30. data/ext/spidermonkey/spidermonkey.c +20 -0
  31. data/ext/spidermonkey/spidermonkey.h +29 -0
  32. data/js/johnson/browser.js +9 -0
  33. data/js/johnson/browser/env.js +687 -0
  34. data/js/johnson/browser/jquery.js +3444 -0
  35. data/js/johnson/browser/xmlsax.js +1564 -0
  36. data/js/johnson/browser/xmlw3cdom.js +4189 -0
  37. data/js/johnson/cli.js +30 -0
  38. data/js/johnson/prelude.js +80 -0
  39. data/js/johnson/template.js +29 -0
  40. data/lib/hoe.rb +748 -0
  41. data/lib/johnson.rb +46 -0
  42. data/lib/johnson/cli.rb +7 -0
  43. data/lib/johnson/cli/options.rb +56 -0
  44. data/lib/johnson/error.rb +4 -0
  45. data/lib/johnson/nodes.rb +7 -0
  46. data/lib/johnson/nodes/binary_node.rb +64 -0
  47. data/lib/johnson/nodes/for.rb +14 -0
  48. data/lib/johnson/nodes/for_in.rb +12 -0
  49. data/lib/johnson/nodes/function.rb +13 -0
  50. data/lib/johnson/nodes/list.rb +27 -0
  51. data/lib/johnson/nodes/node.rb +68 -0
  52. data/lib/johnson/nodes/ternary_node.rb +20 -0
  53. data/lib/johnson/parser.rb +21 -0
  54. data/lib/johnson/parser/syntax_error.rb +13 -0
  55. data/lib/johnson/runtime.rb +55 -0
  56. data/lib/johnson/spidermonkey/context.rb +10 -0
  57. data/lib/johnson/spidermonkey/debugger.rb +67 -0
  58. data/lib/johnson/spidermonkey/immutable_node.rb +280 -0
  59. data/lib/johnson/spidermonkey/js_land_proxy.rb +62 -0
  60. data/lib/johnson/spidermonkey/mutable_tree_visitor.rb +233 -0
  61. data/lib/johnson/spidermonkey/ruby_land_proxy.rb +52 -0
  62. data/lib/johnson/spidermonkey/runtime.rb +94 -0
  63. data/lib/johnson/version.rb +4 -0
  64. data/lib/johnson/visitable.rb +16 -0
  65. data/lib/johnson/visitors.rb +4 -0
  66. data/lib/johnson/visitors/dot_visitor.rb +167 -0
  67. data/lib/johnson/visitors/ecma_visitor.rb +315 -0
  68. data/lib/johnson/visitors/enumerating_visitor.rb +115 -0
  69. data/lib/johnson/visitors/sexp_visitor.rb +172 -0
  70. data/lib/rails/init.rb +37 -0
  71. data/test/assets/index.html +38 -0
  72. data/test/assets/jquery_test.html +186 -0
  73. data/test/helper.rb +58 -0
  74. data/test/johnson/browser_test.rb +38 -0
  75. data/test/johnson/conversions/array_test.rb +32 -0
  76. data/test/johnson/conversions/boolean_test.rb +17 -0
  77. data/test/johnson/conversions/callable_test.rb +34 -0
  78. data/test/johnson/conversions/file_test.rb +15 -0
  79. data/test/johnson/conversions/nil_test.rb +20 -0
  80. data/test/johnson/conversions/number_test.rb +34 -0
  81. data/test/johnson/conversions/regexp_test.rb +24 -0
  82. data/test/johnson/conversions/string_test.rb +26 -0
  83. data/test/johnson/conversions/struct_test.rb +15 -0
  84. data/test/johnson/conversions/symbol_test.rb +19 -0
  85. data/test/johnson/conversions/thread_test.rb +24 -0
  86. data/test/johnson/error_test.rb +9 -0
  87. data/test/johnson/extensions_test.rb +56 -0
  88. data/test/johnson/nodes/array_literal_test.rb +57 -0
  89. data/test/johnson/nodes/array_node_test.rb +26 -0
  90. data/test/johnson/nodes/binary_node_test.rb +61 -0
  91. data/test/johnson/nodes/bracket_access_test.rb +16 -0
  92. data/test/johnson/nodes/delete_test.rb +11 -0
  93. data/test/johnson/nodes/do_while_test.rb +12 -0
  94. data/test/johnson/nodes/dot_accessor_test.rb +15 -0
  95. data/test/johnson/nodes/export_test.rb +9 -0
  96. data/test/johnson/nodes/for_test.rb +54 -0
  97. data/test/johnson/nodes/function_test.rb +71 -0
  98. data/test/johnson/nodes/if_test.rb +41 -0
  99. data/test/johnson/nodes/import_test.rb +13 -0
  100. data/test/johnson/nodes/label_test.rb +19 -0
  101. data/test/johnson/nodes/object_literal_test.rb +110 -0
  102. data/test/johnson/nodes/return_test.rb +16 -0
  103. data/test/johnson/nodes/semi_test.rb +8 -0
  104. data/test/johnson/nodes/switch_test.rb +55 -0
  105. data/test/johnson/nodes/ternary_test.rb +25 -0
  106. data/test/johnson/nodes/throw_test.rb +9 -0
  107. data/test/johnson/nodes/try_node_test.rb +59 -0
  108. data/test/johnson/nodes/typeof_test.rb +11 -0
  109. data/test/johnson/nodes/unary_node_test.rb +23 -0
  110. data/test/johnson/nodes/void_test.rb +11 -0
  111. data/test/johnson/nodes/while_test.rb +26 -0
  112. data/test/johnson/nodes/with_test.rb +10 -0
  113. data/test/johnson/prelude_test.rb +56 -0
  114. data/test/johnson/runtime_test.rb +46 -0
  115. data/test/johnson/spidermonkey/context_test.rb +21 -0
  116. data/test/johnson/spidermonkey/immutable_node_test.rb +34 -0
  117. data/test/johnson/spidermonkey/js_land_proxy_test.rb +236 -0
  118. data/test/johnson/spidermonkey/ruby_land_proxy_test.rb +225 -0
  119. data/test/johnson/spidermonkey/runtime_test.rb +17 -0
  120. data/test/johnson/version_test.rb +13 -0
  121. data/test/johnson/visitors/dot_visitor_test.rb +39 -0
  122. data/test/johnson/visitors/enumerating_visitor_test.rb +12 -0
  123. data/test/johnson_test.rb +16 -0
  124. data/test/jquery_units/test.js +27 -0
  125. data/test/jquery_units/test_helper.js +197 -0
  126. data/test/jquery_units/units/ajax.js +795 -0
  127. data/test/jquery_units/units/core.js +1563 -0
  128. data/test/jquery_units/units/event.js +299 -0
  129. data/test/jquery_units/units/fx.js +427 -0
  130. data/test/jquery_units/units/offset.js +112 -0
  131. data/test/jquery_units/units/selector.js +224 -0
  132. data/test/jspec/helper.js +7 -0
  133. data/test/jspec/jspec.js +192 -0
  134. data/test/jspec/simple_spec.js +68 -0
  135. data/test/parser_test.rb +276 -0
  136. data/todo/.keep +0 -0
  137. metadata +501 -0
@@ -0,0 +1,19 @@
1
+ #ifndef JOHNSON_SPIDERMONKEY_CONTEXT_H
2
+ #define JOHNSON_SPIDERMONKEY_CONTEXT_H
3
+
4
+ #include "spidermonkey.h"
5
+
6
+ #define MAX_EXCEPTION_MESSAGE_SIZE 2048
7
+
8
+ typedef struct {
9
+ JSContext *js;
10
+
11
+ jsval ex; // an exception value
12
+ char msg[MAX_EXCEPTION_MESSAGE_SIZE]; // the 'backup' message
13
+
14
+ } JohnsonContext;
15
+
16
+ void init_Johnson_SpiderMonkey_Context(VALUE spidermonkey);
17
+ VALUE Johnson_SpiderMonkey_JSLandProxy();
18
+
19
+ #endif
@@ -0,0 +1,286 @@
1
+ #include "conversions.h"
2
+ #include "js_land_proxy.h"
3
+ #include "ruby_land_proxy.h"
4
+
5
+ DEFINE_RUBY_WRAPPER(convert_to_ruby, convert_to_ruby, ARGLIST2(runtime, js_value))
6
+
7
+ DECLARE_RUBY_WRAPPER(rb_funcall_0, VALUE obj; ID sym; int argc)
8
+ DEFINE_RUBY_WRAPPER(rb_funcall_0, rb_funcall, ARGLIST3(obj, sym, argc))
9
+
10
+ DECLARE_RUBY_WRAPPER(rb_funcall_2, VALUE obj; ID sym; int argc; VALUE a; VALUE b)
11
+ DEFINE_RUBY_WRAPPER(rb_funcall_2, rb_funcall, ARGLIST5(obj, sym, argc, a, b))
12
+
13
+ static JSBool convert_float_or_bignum_to_js(JohnsonRuntime* runtime, VALUE float_or_bignum, jsval* retval)
14
+ {
15
+ JSContext * context = johnson_get_current_context(runtime);
16
+ return JS_NewDoubleValue(context, NUM2DBL(float_or_bignum), retval);
17
+ }
18
+
19
+ static JSBool convert_symbol_to_js(JohnsonRuntime* runtime, VALUE symbol, jsval* retval)
20
+ {
21
+ JSContext * context = johnson_get_current_context(runtime);
22
+ PREPARE_JROOTS(context, 2);
23
+
24
+ VALUE to_s = CALL_RUBY_WRAPPER(rb_funcall_0, symbol, rb_intern("to_s"), 0);
25
+ jsval name = STRING_TO_JSVAL(JS_NewStringCopyN(context, StringValuePtr(to_s), (unsigned) StringValueLen(to_s)));
26
+
27
+ JROOT(name);
28
+
29
+ // calls Johnson.symbolize(name) in JS-land. See lib/prelude.js
30
+
31
+ jsval nsJohnson;
32
+ JCHECK(JS_GetProperty(context, runtime->global, "Johnson", &nsJohnson));
33
+ JROOT(nsJohnson);
34
+
35
+ JCHECK(JS_CallFunctionName(context, JSVAL_TO_OBJECT(nsJohnson), "symbolize", 1, &name, retval));
36
+
37
+ JRETURN;
38
+ }
39
+
40
+ static JSBool convert_regexp_to_js(JohnsonRuntime* runtime, VALUE regexp, jsval* retval)
41
+ {
42
+ JSContext * context = johnson_get_current_context(runtime);
43
+ VALUE source = rb_funcall(regexp, rb_intern("source"), 0);
44
+ int options = NUM2INT(rb_funcall(regexp, rb_intern("options"), 0));
45
+
46
+ JSObject* obj = JS_NewRegExpObject(context,
47
+ StringValuePtr(source),
48
+ (unsigned) StringValueLen(source),
49
+ (unsigned) options);
50
+
51
+ if (obj) {
52
+ *retval = OBJECT_TO_JSVAL(obj);
53
+ return JS_TRUE;
54
+ } else {
55
+ return JS_FALSE;
56
+ }
57
+ }
58
+
59
+ JSBool convert_to_js(JohnsonRuntime* runtime, VALUE ruby, jsval* retval)
60
+ {
61
+ JSContext * context = johnson_get_current_context(runtime);
62
+ switch(TYPE(ruby))
63
+ {
64
+ case T_NIL:
65
+ *retval = JSVAL_NULL;
66
+ return JS_TRUE;
67
+
68
+ case T_TRUE:
69
+ *retval = JSVAL_TRUE;
70
+ return JS_TRUE;
71
+
72
+ case T_FALSE:
73
+ *retval = JSVAL_FALSE;
74
+ return JS_TRUE;
75
+
76
+ case T_STRING:
77
+ {
78
+ JSString* str = JS_NewStringCopyN(context, StringValuePtr(ruby), (unsigned) StringValueLen(ruby));
79
+ if (str) {
80
+ *retval = STRING_TO_JSVAL(str);
81
+ return JS_TRUE;
82
+ } else {
83
+ return JS_FALSE;
84
+ }
85
+ }
86
+
87
+ case T_FIXNUM:
88
+ *retval = INT_TO_JSVAL(NUM2INT(ruby));
89
+ return JS_TRUE;
90
+
91
+ case T_FLOAT:
92
+ case T_BIGNUM:
93
+ return convert_float_or_bignum_to_js(runtime, ruby, retval);
94
+
95
+ case T_SYMBOL:
96
+ return convert_symbol_to_js(runtime, ruby, retval);
97
+
98
+ case T_CLASS:
99
+ case T_ARRAY:
100
+ case T_HASH:
101
+ case T_MODULE:
102
+ case T_FILE:
103
+ case T_STRUCT:
104
+ case T_OBJECT:
105
+ return make_js_land_proxy(runtime, ruby, retval);
106
+
107
+ case T_REGEXP:
108
+ return convert_regexp_to_js(runtime, ruby, retval);
109
+
110
+ case T_DATA: // HEY! keep T_DATA last for fall-through
111
+ if (ruby_value_is_proxy(ruby))
112
+ return unwrap_ruby_land_proxy(runtime, ruby, retval);
113
+
114
+ // If we can't identify the object, just wrap it
115
+ return make_js_land_proxy(runtime, ruby, retval);
116
+
117
+ default:
118
+ rb_raise(rb_eRuntimeError, "unknown ruby type in switch");
119
+ }
120
+
121
+ *retval = JSVAL_NULL;
122
+ return JS_TRUE;
123
+ }
124
+
125
+ VALUE convert_js_string_to_ruby(JohnsonRuntime* runtime, JSString* str)
126
+ {
127
+ JSContext * context = johnson_get_current_context(runtime);
128
+ PREPARE_RUBY_JROOTS(context, 1);
129
+ JROOT(str);
130
+ char* bytes = JS_GetStringBytes(str);
131
+ JCHECK(bytes);
132
+ JRETURN_RUBY(rb_str_new(bytes, (signed)JS_GetStringLength(str)));
133
+ }
134
+
135
+ static VALUE convert_regexp_to_ruby(JohnsonRuntime* runtime, jsval regexp)
136
+ {
137
+ JSContext * context = johnson_get_current_context(runtime);
138
+ PREPARE_RUBY_JROOTS(context, 1);
139
+ JROOT(regexp);
140
+ JSRegExp* re = (JSRegExp*)JS_GetPrivate(context, JSVAL_TO_OBJECT(regexp));
141
+
142
+ JRETURN_RUBY(CALL_RUBY_WRAPPER(rb_funcall_2, rb_cRegexp, rb_intern("new"), 2,
143
+ convert_js_string_to_ruby(runtime, re->source),
144
+ INT2NUM(re->flags)));
145
+ }
146
+
147
+ static bool js_value_is_regexp(JohnsonRuntime* runtime, jsval maybe_regexp)
148
+ {
149
+ JSContext * context = johnson_get_current_context(runtime);
150
+ PREPARE_RUBY_JROOTS(context, 1);
151
+ JROOT(maybe_regexp);
152
+ JSBool result = JS_InstanceOf(context, JSVAL_TO_OBJECT(maybe_regexp), &js_RegExpClass, NULL);
153
+ JRETURN_RUBY(result ? true : false);
154
+ }
155
+
156
+ static bool js_value_is_symbol(JohnsonRuntime* runtime, jsval maybe_symbol)
157
+ {
158
+ jsval nsJohnson, cSymbol;
159
+ JSContext * context = johnson_get_current_context(runtime);
160
+
161
+ PREPARE_RUBY_JROOTS(context, 3);
162
+ JROOT(maybe_symbol);
163
+
164
+ JCHECK(JS_GetProperty(context, runtime->global, "Johnson", &nsJohnson));
165
+ if (!JSVAL_IS_OBJECT(nsJohnson))
166
+ JERROR("Unable to retrieve Johnson from JSLand");
167
+ JROOT(nsJohnson);
168
+
169
+ JCHECK(JS_GetProperty(context, JSVAL_TO_OBJECT(nsJohnson), "Symbol", &cSymbol));
170
+ if (!JSVAL_IS_OBJECT(cSymbol))
171
+ JERROR("Unable to retrieve Johnson.Symbol from JSLand");
172
+ JROOT(cSymbol);
173
+
174
+ JSBool is_a_symbol;
175
+ JCHECK(JS_HasInstance(context, JSVAL_TO_OBJECT(cSymbol), maybe_symbol, &is_a_symbol));
176
+
177
+ JRETURN_RUBY(is_a_symbol != JS_FALSE);
178
+ }
179
+
180
+ VALUE convert_to_ruby(JohnsonRuntime* runtime, jsval js)
181
+ {
182
+ if (JSVAL_NULL == js) return Qnil;
183
+
184
+ JSContext * context = johnson_get_current_context(runtime);
185
+
186
+ PREPARE_RUBY_JROOTS(context, 1);
187
+ JROOT(js);
188
+
189
+ switch (JS_TypeOfValue(context, js))
190
+ {
191
+ case JSTYPE_VOID:
192
+ JRETURN_RUBY(Qnil);
193
+
194
+ case JSTYPE_FUNCTION:
195
+ case JSTYPE_OBJECT:
196
+ if (OBJECT_TO_JSVAL(runtime->global) == js)
197
+ // global gets special treatment, since the Prelude might not be loaded
198
+ JRETURN_RUBY(make_ruby_land_proxy(runtime, js, "GlobalProxy"));
199
+
200
+ // this conditional requires the Prelude
201
+ if (js_value_is_symbol(runtime, js))
202
+ JRETURN_RUBY(ID2SYM(rb_intern(JS_GetStringBytes(JS_ValueToString(context, js)))));
203
+
204
+ if (js_value_is_proxy(runtime, js))
205
+ JRETURN_RUBY(unwrap_js_land_proxy(runtime, js));
206
+
207
+ if (js_value_is_regexp(runtime, js))
208
+ JRETURN_RUBY(convert_regexp_to_ruby(runtime, js));
209
+
210
+ JRETURN_RUBY(make_ruby_land_proxy(runtime, js, "RubyLandProxy"));
211
+
212
+ case JSTYPE_BOOLEAN:
213
+ JRETURN_RUBY(JSVAL_TRUE == js ? Qtrue : Qfalse);
214
+
215
+ case JSTYPE_STRING:
216
+ JRETURN_RUBY(convert_js_string_to_ruby(runtime, JSVAL_TO_STRING(js)));
217
+
218
+ case JSTYPE_NUMBER:
219
+ if (JSVAL_IS_INT(js)) JRETURN_RUBY(INT2FIX(JSVAL_TO_INT(js)));
220
+ else JRETURN_RUBY(rb_float_new(*JSVAL_TO_DOUBLE(js)));
221
+
222
+ default:
223
+ JERROR("unknown js type in switch");
224
+ }
225
+
226
+ JRETURN_RUBY(Qnil);
227
+ }
228
+
229
+ NORETURN(void) raise_js_error_in_ruby(JohnsonRuntime* runtime)
230
+ {
231
+ JSContext * context = johnson_get_current_context(runtime);
232
+ JohnsonContext * johnson_context = OUR_CONTEXT(context);
233
+ if (JS_IsExceptionPending(context))
234
+ {
235
+ assert(JS_GetPendingException(context, &(johnson_context->ex)));
236
+ JS_AddNamedRoot(context, &(johnson_context->ex), "raise_js_error_in_ruby");
237
+ JS_ClearPendingException(context);
238
+ JS_RemoveRoot(context, &(johnson_context->ex));
239
+ }
240
+
241
+ VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(runtime->js);
242
+ if (johnson_context->ex)
243
+ RAISE_JS_ERROR(ruby_runtime, johnson_context->ex);
244
+
245
+ // FIXME: I don't think this is needed, it should
246
+ // be done on the Ruby side.
247
+ if (!johnson_context->msg)
248
+ rb_raise(rb_eRuntimeError, "Unknown JavaScriptError");
249
+
250
+ // FIXME: I don't think this can ever happen....
251
+ rb_raise(rb_eRuntimeError, johnson_context->msg);
252
+ }
253
+
254
+ #define TAG_RAISE 0x6
255
+ #define TAG_THROW 0x7
256
+
257
+ JSBool report_ruby_error_in_js(JohnsonRuntime* runtime, int state, VALUE old_errinfo)
258
+ {
259
+ JSContext * context = johnson_get_current_context(runtime);
260
+ assert(state);
261
+ switch (state)
262
+ {
263
+ case TAG_RAISE:
264
+ {
265
+ VALUE local_error = ruby_errinfo;
266
+ jsval js_err;
267
+ ruby_errinfo = old_errinfo;
268
+ if (!convert_to_js(runtime, local_error, &js_err))
269
+ return JS_FALSE;
270
+ JS_SetPendingException(context, js_err);
271
+ return JS_FALSE;
272
+ }
273
+
274
+ case TAG_THROW:
275
+ // FIXME: This should be propagated to JS... as an exception?
276
+
277
+ default:
278
+ {
279
+ JSString* str = JS_NewStringCopyZ(context, "Unexpected longjmp from ruby!");
280
+ if (str)
281
+ JS_SetPendingException(context, STRING_TO_JSVAL(str));
282
+ return JS_FALSE;
283
+ }
284
+ }
285
+ }
286
+
@@ -0,0 +1,18 @@
1
+ #ifndef JOHNSON_SPIDERMONKEY_CONVERSIONS_H
2
+ #define JOHNSON_SPIDERMONKEY_CONVERSIONS_H
3
+
4
+ #include "spidermonkey.h"
5
+ #include "context.h"
6
+ #include "runtime.h"
7
+
8
+ DECLARE_RUBY_WRAPPER(convert_to_ruby, JohnsonRuntime* runtime; jsval js_value)
9
+ #define CONVERT_TO_RUBY(runtime, js) CALL_RUBY_WRAPPER(convert_to_ruby, runtime, js)
10
+
11
+ JSBool convert_to_js(JohnsonRuntime* runtime, VALUE ruby, jsval* retval);
12
+ VALUE convert_to_ruby(JohnsonRuntime* runtime, jsval js);
13
+ VALUE convert_js_string_to_ruby(JohnsonRuntime* runtime, JSString* str);
14
+
15
+ NORETURN(void raise_js_error_in_ruby(JohnsonRuntime* runtime));
16
+ JSBool report_ruby_error_in_js(JohnsonRuntime* runtime, int state, VALUE old_errinfo);
17
+
18
+ #endif
@@ -0,0 +1,208 @@
1
+ #include "debugger.h"
2
+ #include "context.h"
3
+ #include "conversions.h"
4
+ #include "immutable_node.h"
5
+
6
+ static VALUE frame_pc(VALUE UNUSED(self), VALUE context, VALUE frame)
7
+ {
8
+ JSContext * js = NULL;
9
+ JSStackFrame * fp = NULL;
10
+ Data_Get_Struct(context, JSContext, js);
11
+ Data_Get_Struct(frame, JSStackFrame, fp);
12
+ return Data_Wrap_Struct(rb_cObject, NULL, NULL, JS_GetFramePC(js, fp));
13
+ }
14
+
15
+ static VALUE line_number(VALUE UNUSED(self), VALUE context, VALUE script, VALUE bytecode)
16
+ {
17
+ JSContext * js = NULL;
18
+ JSScript * js_script = NULL;
19
+ jsbytecode * js_bytecode = NULL;
20
+
21
+ Data_Get_Struct(context, JSContext, js);
22
+ Data_Get_Struct(script, JSScript, js_script);
23
+ Data_Get_Struct(bytecode, jsbytecode, js_bytecode);
24
+
25
+ return INT2NUM(JS_PCToLineNumber(js, js_script, js_bytecode));
26
+ }
27
+
28
+ static VALUE file_name(VALUE UNUSED(self), VALUE context, VALUE script)
29
+ {
30
+ JSContext * js = NULL;
31
+ JSScript * js_script = NULL;
32
+
33
+ Data_Get_Struct(context, JSContext, js);
34
+ Data_Get_Struct(script, JSScript, js_script);
35
+
36
+ return rb_str_new2(JS_GetScriptFilename(js, js_script));
37
+ }
38
+
39
+ static JSTrapStatus interrupt_handler(JSContext *js, JSScript *script,
40
+ jsbytecode *pc, jsval *UNUSED(rval), void *rb)
41
+ {
42
+ assert(js);
43
+ assert(rb);
44
+ assert(pc);
45
+
46
+ VALUE self = (VALUE)rb;
47
+ VALUE rb_cx = Data_Wrap_Struct(rb_cObject, NULL, NULL, js);
48
+ VALUE rb_script = Data_Wrap_Struct(rb_cObject, NULL, NULL, script);
49
+ VALUE rb_pc = Data_Wrap_Struct(rb_cObject, NULL, NULL, pc);
50
+
51
+ return NUM2INT(rb_funcall(self, rb_intern("interrupt_handler"), 3,
52
+ rb_cx,
53
+ rb_script,
54
+ rb_pc
55
+ ));
56
+ }
57
+
58
+ static void new_script_hook(JSContext *UNUSED(js),
59
+ const char *filename,
60
+ uintN lineno,
61
+ JSScript *UNUSED(script),
62
+ JSFunction *UNUSED(fun),
63
+ void *rb)
64
+ {
65
+ VALUE self = (VALUE)rb;
66
+ VALUE rb_filename = rb_str_new2(filename);
67
+ VALUE rb_linenum = UINT2NUM(lineno);
68
+
69
+ /* FIXME: Pass the rest of this crap to the debugger? */
70
+ rb_funcall(self, rb_intern("new_script_hook"), 2, rb_filename, rb_linenum);
71
+ }
72
+
73
+ static void destroy_script_hook(JSContext *UNUSED(js),
74
+ JSScript *UNUSED(script),
75
+ void *rb)
76
+ {
77
+ VALUE self = (VALUE)rb;
78
+ rb_funcall(self, rb_intern("destroy_script_hook"), 0);
79
+ }
80
+
81
+ static JSTrapStatus debugger_handler(JSContext *js, JSScript *script,
82
+ jsbytecode *pc, jsval *UNUSED(rval), void *rb)
83
+ {
84
+ assert(js);
85
+ assert(rb);
86
+ assert(pc);
87
+
88
+ VALUE self = (VALUE)rb;
89
+ VALUE rb_cx = Data_Wrap_Struct(rb_cObject, NULL, NULL, js);
90
+ VALUE rb_script = Data_Wrap_Struct(rb_cObject, NULL, NULL, script);
91
+ VALUE rb_pc = Data_Wrap_Struct(rb_cObject, NULL, NULL, pc);
92
+
93
+ return NUM2INT(rb_funcall(self, rb_intern("debugger_handler"), 3,
94
+ rb_cx,
95
+ rb_script,
96
+ rb_pc
97
+ ));
98
+ }
99
+
100
+ static void source_handler(const char *filename, uintN lineno,
101
+ jschar *str, size_t length,
102
+ void **UNUSED(listenerTSData), void *rb)
103
+ {
104
+ VALUE self = (VALUE)rb;
105
+ VALUE rb_filename = rb_str_new2(filename);
106
+ VALUE rb_lineno = UINT2NUM(lineno);
107
+ VALUE rb_str = rb_str_new((char *)str, (signed)(length * sizeof(jschar)));
108
+
109
+ rb_funcall(self, rb_intern("source_handler"), 3, rb_filename, rb_lineno, rb_str);
110
+ }
111
+
112
+ static void * execute_hook(JSContext *js, JSStackFrame *fp, JSBool before,
113
+ JSBool *ok, void *rb)
114
+ {
115
+ VALUE self = (VALUE)rb;
116
+ VALUE rb_before = JS_TRUE == before ? Qtrue : Qfalse;
117
+ VALUE rb_ok = ok ? Qtrue : Qfalse;
118
+ VALUE rb_js = Data_Wrap_Struct(rb_cObject, NULL, NULL, (void *)js);
119
+ VALUE rb_fp = Data_Wrap_Struct(rb_cObject, NULL, NULL, (void *)fp);
120
+
121
+ rb_funcall(self, rb_intern("execute_hook"), 4, rb_js,rb_fp,rb_before,rb_ok);
122
+ return rb;
123
+ }
124
+
125
+ static void * call_hook(JSContext *UNUSED(js), JSStackFrame *UNUSED(fp), JSBool before,
126
+ JSBool *ok, void *rb)
127
+ {
128
+ VALUE self = (VALUE)rb;
129
+ VALUE rb_before = before ? Qtrue : Qfalse;
130
+ VALUE rb_ok = ok ? Qtrue : Qfalse;
131
+
132
+ rb_funcall(self, rb_intern("call_hook"), 2, rb_before, rb_ok);
133
+ return rb;
134
+ }
135
+
136
+ static void object_hook(JSContext *js, JSObject *obj, JSBool isNew, void *rb)
137
+ {
138
+ assert(rb);
139
+ assert(js);
140
+ assert(obj);
141
+ VALUE self = (VALUE)rb;
142
+
143
+ VALUE rb_is_new = isNew ? Qtrue : Qfalse;
144
+
145
+ rb_funcall(self, rb_intern("object_hook"), 1, rb_is_new);
146
+ }
147
+
148
+ static JSTrapStatus throw_hook(JSContext *UNUSED(js), JSScript *UNUSED(script),
149
+ jsbytecode *pc, jsval *UNUSED(rval), void *rb)
150
+ {
151
+ VALUE self = (VALUE)rb;
152
+ VALUE rb_bytecode = jsop_to_symbol(*pc);
153
+ return NUM2INT(rb_funcall(self, rb_intern("throw_hook"), 1, rb_bytecode));
154
+ }
155
+
156
+ static JSBool debug_error_hook(JSContext *UNUSED(js), const char *message,
157
+ JSErrorReport *UNUSED(report), void *rb)
158
+ {
159
+ VALUE self = (VALUE)rb;
160
+ VALUE rb_message = rb_str_new2(message);
161
+ rb_funcall(self, rb_intern("debug_error_hook"), 1, rb_message);
162
+ return JS_TRUE;
163
+ }
164
+
165
+ static VALUE allocate(VALUE klass)
166
+ {
167
+ JSDebugHooks* debug = calloc(1, sizeof(JSDebugHooks));
168
+ VALUE self = Data_Wrap_Struct(klass, 0, 0, debug);
169
+
170
+ debug->interruptHandler = interrupt_handler;
171
+ debug->interruptHandlerData = (void *)self;
172
+ debug->newScriptHook = new_script_hook;
173
+ debug->newScriptHookData = (void *)self;
174
+ debug->destroyScriptHook = destroy_script_hook;
175
+ debug->destroyScriptHookData = (void *)self;
176
+ debug->debuggerHandler = debugger_handler;
177
+ debug->debuggerHandlerData = (void *)self;
178
+ debug->sourceHandler = source_handler;
179
+ debug->sourceHandlerData = (void *)self;
180
+ debug->executeHook = execute_hook;
181
+ debug->executeHookData = (void *)self;
182
+ debug->callHook = call_hook;
183
+ debug->callHookData = (void *)self;
184
+ debug->objectHook = object_hook;
185
+ debug->objectHookData = (void *)self;
186
+ debug->throwHook = throw_hook;
187
+ debug->throwHookData = (void *)self;
188
+ debug->debugErrorHook = debug_error_hook;
189
+ debug->debugErrorHookData = (void *)self;
190
+
191
+ return self;
192
+ }
193
+
194
+ void init_Johnson_SpiderMonkey_Debugger(VALUE spidermonkey)
195
+ {
196
+ /* HACK: These comments are *only* to make RDoc happy.
197
+ VALUE johnson = rb_define_module("Johnson");
198
+ VALUE spidermonkey = rb_define_module_under(johnson, "SpiderMonkey");
199
+ */
200
+
201
+ /* This is the debugging hooks used with SpiderMonkey. */
202
+ VALUE debugger = rb_define_class_under(spidermonkey, "Debugger", rb_cObject);
203
+ rb_define_private_method(debugger, "frame_pc", frame_pc, 2);
204
+ rb_define_private_method(debugger, "line_number", line_number, 3);
205
+ rb_define_private_method(debugger, "file_name", file_name, 2);
206
+
207
+ rb_define_alloc_func(debugger, allocate);
208
+ }