Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 610 lines (464 sloc) 18.593 kb
e18f74d @jbarnette Renaming proxies for clarity.
authored
1 #include "js_land_proxy.h"
dc42336 @matthewd Deal with a couple of minor warnings.
matthewd authored
2 #include "conversions.h"
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
3
4 static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval);
5 static JSBool set(JSContext* context, JSObject* obj, jsval id, jsval* retval);
b77b3ad @jbarnette Make sure multiple proxy classes are supported.
authored
6 static JSBool construct(JSContext* js_context, JSObject* obj, uintN argc, jsval* argv, jsval* retval);
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
7 static JSBool resolve(JSContext *js_context, JSObject *obj, jsval id, uintN flags, JSObject **objp);
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
8 static JSBool call(JSContext* js_context, JSObject* obj, uintN argc, jsval* argv, jsval* retval);
b77b3ad @jbarnette Make sure multiple proxy classes are supported.
authored
9 static void finalize(JSContext* context, JSObject* obj);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
10
510f1b5 @jbarnette Fix some vestigal renaming issues.
authored
11 static JSClass JSLandProxyClass = {
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
12 "JSLandProxy", JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE,
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
13 JS_PropertyStub,
14 JS_PropertyStub,
15 get,
16 set,
17 JS_EnumerateStub,
d3286d8 @jbarnette Fixing compiler warnings.
authored
18 (JSResolveOp) resolve,
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
19 JS_ConvertStub,
20 finalize
21 };
22
b77b3ad @jbarnette Make sure multiple proxy classes are supported.
authored
23 static JSClass JSLandClassProxyClass = {
24 "JSLandClassProxy", JSCLASS_HAS_PRIVATE,
25 JS_PropertyStub,
26 JS_PropertyStub,
27 get,
28 set,
29 JS_EnumerateStub,
30 JS_ResolveStub,
31 JS_ConvertStub,
32 finalize,
33 NULL,
34 NULL,
35 NULL,
36 construct
37 };
38
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
39 static JSClass JSLandCallableProxyClass = {
40 "JSLandCallableProxy", JSCLASS_HAS_PRIVATE,
41 JS_PropertyStub,
42 JS_PropertyStub,
f518879 @jbarnette Any Rubyland object that responds to call can be called as a function in...
authored
43 get,
44 set,
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
45 JS_EnumerateStub,
46 JS_ResolveStub,
47 JS_ConvertStub,
48 finalize,
49 NULL,
50 NULL,
51 call
52 };
53
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
54 static VALUE call_ruby_from_js_invoke(VALUE args)
55 {
56 VALUE self = rb_ary_pop(args);
57 VALUE id = rb_ary_pop(args);
58 return rb_apply(self, SYM2ID(id), args);
59 }
60
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
61 JSBool call_ruby_from_js_va(JohnsonRuntime* runtime, VALUE* result, VALUE self, ID id, int argc, va_list va)
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
62 {
63 VALUE old_errinfo = ruby_errinfo;
64 VALUE args = rb_ary_new2(argc + 2);
65
66 int i;
67 for(i = 0; i < argc; i++)
68 rb_ary_store(args, i, va_arg(va, VALUE));
69
70 rb_ary_store(args, argc, ID2SYM(id));
71 rb_ary_store(args, argc + 1, self);
72
73 int state;
74 *result = rb_protect(call_ruby_from_js_invoke, args, &state);
75
15f8763 @matthewd Deal with a couple of places that Ruby exceptions can leak through JSLan...
matthewd authored
76 if (state)
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
77 return report_ruby_error_in_js(runtime, state, old_errinfo);
15f8763 @matthewd Deal with a couple of places that Ruby exceptions can leak through JSLan...
matthewd authored
78
79 return JS_TRUE;
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
80 }
81
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
82 JSBool call_ruby_from_js(JohnsonRuntime* runtime, jsval* retval, VALUE self, ID id, int argc, ...)
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
83 {
84 VALUE result;
85 va_list va;
86 va_start(va, argc);
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
87 JSBool okay = call_ruby_from_js_va(runtime, &result, self, id, argc, va);
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
88 va_end(va);
89 if (!okay) return JS_FALSE;
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
90 return retval ? convert_to_js(runtime, result, retval) : JS_TRUE;
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
91 }
92
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
93 JSBool call_ruby_from_js2(JohnsonRuntime* runtime, VALUE* retval, VALUE self, ID id, int argc, ...)
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
94 {
95 va_list va;
96 va_start(va, argc);
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
97 JSBool okay = call_ruby_from_js_va(runtime, retval, self, id, argc, va);
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
98 va_end(va);
99 return okay;
100 }
101
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
102 static bool autovivified_p(VALUE UNUSED(ruby_context), VALUE self, char* name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
103 {
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
104 return RTEST(rb_funcall(Johnson_SpiderMonkey_JSLandProxy(), rb_intern("autovivified?"), 2,
105 self, rb_str_new2(name)));
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
106 }
107
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
108 static bool const_p(VALUE self, char* name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
109 {
110 return rb_obj_is_kind_of(self, rb_cModule)
111 && rb_is_const_id(rb_intern(name))
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
112 && RTEST( rb_funcall(self, rb_intern("const_defined?"), 1, ID2SYM(rb_intern(name))) );
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
113 }
114
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
115 static bool global_p(char* name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
116 {
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
117 return *name == '$' && rb_ary_includes(rb_f_global_variables(), rb_str_new2(name));
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
118 }
119
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
120 static bool method_p(VALUE self, char* name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
121 {
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
122 return RTEST( rb_funcall(self, rb_intern("respond_to?"), 1, ID2SYM(rb_intern(name))) );
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
123 }
124
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
125 static bool attribute_p(VALUE self, char* name)
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
126 {
127 if (!method_p(self, name))
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
128 return false;
5447b86 @matthewd Be a little more certain that we've got a METHOD* before we poke it.
matthewd authored
129
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
130 VALUE rb_id = rb_intern(name);
131 VALUE rb_method = rb_funcall(self, rb_intern("method"), 1, ID2SYM(rb_id));
132
5447b86 @matthewd Be a little more certain that we've got a METHOD* before we poke it.
matthewd authored
133 if (TYPE(rb_method) == T_DATA)
134 {
135 VALUE klass = CLASS_OF(rb_method);
136 if (klass == rb_cMethod)
137 {
138 METHOD* method;
139 Data_Get_Struct(rb_method, METHOD, method);
140
141 if (method && nd_type(method->body) == NODE_IVAR)
142 return true;
143 }
144 }
145
146 return RTEST(rb_funcall(Johnson_SpiderMonkey_JSLandProxy(),
147 rb_intern("js_property?"), 2, self, ID2SYM(rb_id)));
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
148 }
149
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
150 static bool indexable_p(VALUE self)
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
151 {
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
152 return RTEST(rb_funcall(self, rb_intern("respond_to?"), 1, ID2SYM(rb_intern("[]"))));
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
153 }
154
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
155 static bool has_key_p(VALUE self, char* name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
156 {
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
157 return RTEST(rb_funcall(self, rb_intern("respond_to?"), 1, ID2SYM(rb_intern("[]"))))
158 && RTEST(rb_funcall(self, rb_intern("respond_to?"), 1, ID2SYM(rb_intern("key?"))))
159 && RTEST(rb_funcall(self, rb_intern("key?"), 1, rb_str_new2(name)));
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
160 }
161
0746d92 @matthewd Use C99 bool for simple conditionals, and JSBool only for JS engine.
matthewd authored
162 static bool respond_to_p(JSContext* js_context, JSObject* obj, char* name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
163 {
97b8244 @matthewd Root everything in sight.
matthewd authored
164 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
165
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
166 JohnsonContext* context;
167 Data_Get_Struct(ruby_context, JohnsonContext, context);
97b8244 @matthewd Root everything in sight.
matthewd authored
168
169 VALUE self = (VALUE)JS_GetInstancePrivate(
170 context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
171
172 if (!self) return false;
173
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
174 return autovivified_p(ruby_context, self, name)
175 || const_p(self, name)
176 || global_p(name)
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
177 || attribute_p(self, name)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
178 || method_p(self, name)
179 || has_key_p(self, name);
180 }
181
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
182 static jsval evaluate_js_property_expression(JohnsonRuntime * runtime, const char * property, jsval* retval) {
183 JSContext * context = johnson_get_current_context(runtime);
184 return JS_EvaluateScript(context, runtime->global,
bda2acd @matthewd Merged up to removal of vendor/spidermonkey.
matthewd authored
185 property, strlen(property), "johnson:evaluate_js_property_expression", 1,
186 retval);
63b6e8d @tenderlove reducing the iterator code
tenderlove authored
187 }
188
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
189 static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
190 {
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
191 // pull out our Ruby context, which is embedded in js_context
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
192
97b8244 @matthewd Root everything in sight.
matthewd authored
193 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
194
195 // get our struct, which is embedded in ruby_context
196
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
197 JohnsonContext* context;
198 JohnsonRuntime* runtime;
199 Data_Get_Struct(ruby_context, JohnsonContext, context);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
200
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
201 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
202 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
203
204 PREPARE_JROOTS(js_context, 1);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
205 JROOT(id);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
206
207 // get the Ruby object that backs this proxy
208
97b8244 @matthewd Root everything in sight.
matthewd authored
209 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
210
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
211 // Short-circuit for numeric indexes
212
213 if (JSVAL_IS_INT(id))
214 {
97b8244 @matthewd Root everything in sight.
matthewd authored
215 if (indexable_p(self)) {
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
216 VALUE idx = INT2FIX(JSVAL_TO_INT(id));
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
217 JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("[]"), 1, idx));
97b8244 @matthewd Root everything in sight.
matthewd authored
218 }
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
219
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
220 JRETURN;
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
221 }
222
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
223 char* name = JS_GetStringBytes(JSVAL_TO_STRING(id));
224 VALUE ruby_id = rb_intern(name);
97b8244 @matthewd Root everything in sight.
matthewd authored
225
d45a2d5 @jbarnette Added some notes for the future.
authored
226 // FIXME: we should probably just JS_DefineProperty this, and it shouldn't be enumerable
227
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
228 if (!strcasecmp("__iterator__", name)) {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
229 JCHECK(evaluate_js_property_expression(runtime, "Johnson.Generator.create", retval));
576e1fe #28 #23 arrays work in js land and so does for .. in
Aaron Patterson authored
230 }
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
231
204c021 switching to hash based dynamic properties
Aaron Patterson authored
232 // if the Ruby object has a dynamic js property with a key
233 // matching the property we're looking for, pull the value out of
234 // that map.
78f8bf9 @jbarnette Naming and formatting nitpicks.
authored
235
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
236 else if (autovivified_p(ruby_context, self, name))
204c021 switching to hash based dynamic properties
Aaron Patterson authored
237 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
238 JCHECK(call_ruby_from_js(runtime, retval, Johnson_SpiderMonkey_JSLandProxy(),
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
239 rb_intern("autovivified"), 2, self, rb_str_new2(name)));
204c021 switching to hash based dynamic properties
Aaron Patterson authored
240 }
241
5ccbbfa Constants can be accessed as properties in JSLand.
jbarnette authored
242 // if the Ruby object is a Module or Class and has a matching
243 // const defined, return the converted result of const_get
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
244
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
245 else if (const_p(self, name))
5ccbbfa Constants can be accessed as properties in JSLand.
jbarnette authored
246 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
247 JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("const_get"),
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
248 1, ID2SYM(ruby_id)));
5ccbbfa Constants can be accessed as properties in JSLand.
jbarnette authored
249 }
1f9f150 @tenderlove giving spidermonkey access to ruby constants
tenderlove authored
250
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
251 // otherwise, if it's a global, return the global
252 else if (global_p(name))
1f9f150 @tenderlove giving spidermonkey access to ruby constants
tenderlove authored
253 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
254 JCHECK(convert_to_js(runtime, rb_gv_get(name), retval));
1f9f150 @tenderlove giving spidermonkey access to ruby constants
tenderlove authored
255 }
5ccbbfa Constants can be accessed as properties in JSLand.
jbarnette authored
256
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
257 // otherwise, if the Ruby object has a an attribute method matching
5ccbbfa Constants can be accessed as properties in JSLand.
jbarnette authored
258 // the property we're trying to get, call it and return the converted result
259
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
260 else if (attribute_p(self, name))
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
261 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
262 JCHECK(call_ruby_from_js(runtime, retval, self, ruby_id, 0));
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
263 }
5ccbbfa Constants can be accessed as properties in JSLand.
jbarnette authored
264
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
265 // otherwise, if the Ruby object quacks sorta like a hash (it responds to
266 // "[]" and "key?"), index it by key and return the converted result
267
268 else if (has_key_p(self, name))
269 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
270 JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("[]"), 1, rb_str_new2(name)));
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
271 }
272
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
273 // otherwise, it's a method being accessed as a property, which means
274 // we need to return a lambda
275
cfcba6d @jbarnette Added a few more FIXMEs.
authored
276 // FIXME: this should really wrap the Method for 'name' in a JS class
277 // rather than generating a wrapper Proc
278
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
279 else if (method_p(self, name))
280 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
281 JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("method"), 1, rb_str_new2(name)));
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
282 }
5a4b5f0 @matthewd Do a slightly better job of keeping +id+ rooted as long as we need +name...
matthewd authored
283
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
284 // else it's undefined (JS_VOID) by default
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
285 JRETURN;
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
286 }
287
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
288 // called for lazily resolved properties, which should go away
289 static JSBool get_and_destroy_resolved_property(
290 JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
291 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
292 PREPARE_JROOTS(js_context, 1);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
293 JROOT(id);
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
294 char* name = JS_GetStringBytes(JSVAL_TO_STRING(id));
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
295 JCHECK(JS_DeleteProperty(js_context, obj, name));
296 JCHECK(get(js_context, obj, id, retval));
297 JRETURN;
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
298 }
299
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
300 static JSBool set(JSContext* js_context, JSObject* obj, jsval id, jsval* value)
301 {
97b8244 @matthewd Root everything in sight.
matthewd authored
302 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
303
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
304 JohnsonContext* context;
305 JohnsonRuntime* runtime;
306 Data_Get_Struct(ruby_context, JohnsonContext, context);
307
308 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
309 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
310
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
311 PREPARE_JROOTS(js_context, 2);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
312 JROOT(id);
313 JROOT_PTR(value);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
314
97b8244 @matthewd Root everything in sight.
matthewd authored
315 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
15f8763 @matthewd Deal with a couple of places that Ruby exceptions can leak through JSLan...
matthewd authored
316
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
317 // Short-circuit for numeric indexes
318
319 if (JSVAL_IS_INT(id))
320 {
321 if (indexable_p(self))
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
322 {
323 VALUE idx = INT2FIX(JSVAL_TO_INT(id));
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
324 VALUE val = CONVERT_TO_RUBY(runtime, *value);
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
325
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
326 JCHECK(call_ruby_from_js(runtime, NULL, self, rb_intern("[]="), 2, idx, val));
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
327 }
97b8244 @matthewd Root everything in sight.
matthewd authored
328
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
329 JRETURN;
e0d5bf2 @jbarnette Simple implementation for working numeric array indexes.
authored
330 }
331
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
332 VALUE ruby_key = CONVERT_TO_RUBY(runtime, id);
333 VALUE ruby_value = CONVERT_TO_RUBY(runtime, *value);
97b8244 @matthewd Root everything in sight.
matthewd authored
334
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
335 VALUE setter = rb_str_append(rb_str_new3(ruby_key), rb_str_new2("="));
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
336 VALUE setter_id = rb_intern(StringValueCStr(setter));
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
337
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
338 VALUE settable_p, indexable_p;
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
339 JCHECK(call_ruby_from_js2(runtime, &settable_p, self, rb_intern("respond_to?"), 1, ID2SYM(setter_id)));
340 JCHECK(call_ruby_from_js2(runtime, &indexable_p, self, rb_intern("respond_to?"), 1, ID2SYM(rb_intern("[]="))));
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
341
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
342 if (settable_p)
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
343 {
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
344 VALUE method, arity;
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
345 JCHECK(call_ruby_from_js2(runtime, &method, self, rb_intern("method"), 1, ID2SYM(setter_id)));
346 JCHECK(call_ruby_from_js2(runtime, &arity, method, rb_intern("arity"), 0));
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
347
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
348 // if the Ruby object has a 1-arity method named "property=",
349 // call it with the converted value
350
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
351 if (NUM2INT(arity) == 1)
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
352 JCHECK(call_ruby_from_js(runtime, NULL, self, setter_id, 1, ruby_value));
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
353 }
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
354 else if(indexable_p)
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
355 {
356 // otherwise, if the Ruby object quacks sorta like a hash for assignment
357 // (it responds to "[]="), assign it by key
358
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
359 JCHECK(call_ruby_from_js(runtime, NULL, self, rb_intern("[]="), 2, ruby_key, ruby_value));
78f8bf9 @jbarnette Naming and formatting nitpicks.
authored
360 }
361 else
362 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
363 JCHECK(call_ruby_from_js(runtime, NULL, Johnson_SpiderMonkey_JSLandProxy(), rb_intern("autovivify"),
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
364 3, self, ruby_key, ruby_value));
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
365 }
97b8244 @matthewd Root everything in sight.
matthewd authored
366
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
367 JRETURN;
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
368 }
369
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
370 static JSBool construct(JSContext* js_context, JSObject* UNUSED(obj), uintN argc, jsval* argv, jsval* retval)
b77b3ad @jbarnette Make sure multiple proxy classes are supported.
authored
371 {
97b8244 @matthewd Root everything in sight.
matthewd authored
372 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
373
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
374 JohnsonContext* context;
375 JohnsonRuntime* runtime;
376 Data_Get_Struct(ruby_context, JohnsonContext, context);
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
377
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
378 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
379 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
380
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
381 PREPARE_JROOTS(js_context, 0);
382
383 VALUE klass = CONVERT_TO_RUBY(runtime, JS_ARGV_CALLEE(argv));
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
384 VALUE args = rb_ary_new();
385
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
386 uintN i;
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
387 for (i = 0; i < argc; ++i)
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
388 rb_ary_push(args, CONVERT_TO_RUBY(runtime, argv[i]));
46ffbc1 @jbarnette JSland proxies of Ruby classes have working constructors. [#29 state:res...
authored
389
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
390 JCHECK(call_ruby_from_js(runtime, retval, Johnson_SpiderMonkey_JSLandProxy(),
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
391 rb_intern("send_with_possible_block"), 3, klass, ID2SYM(rb_intern("new")), args));
392 JRETURN;
b77b3ad @jbarnette Make sure multiple proxy classes are supported.
authored
393 }
394
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
395 static JSBool resolve(JSContext *js_context, JSObject *obj, jsval id, uintN UNUSED(flags), JSObject **objp)
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
396 {
97b8244 @matthewd Root everything in sight.
matthewd authored
397 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
398
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
399 JohnsonContext* context;
400 Data_Get_Struct(ruby_context, JohnsonContext, context);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
401
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
402 PREPARE_JROOTS(js_context, 1);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
403 JROOT(id);
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
404
405 char* name = JS_GetStringBytes(JS_ValueToString(js_context, id));
97b8244 @matthewd Root everything in sight.
matthewd authored
406
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
407 if (respond_to_p(js_context, obj, name))
408 {
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
409 JCHECK(JS_DefineProperty(js_context, obj, name, JSVAL_VOID,
410 get_and_destroy_resolved_property, set, JSPROP_ENUMERATE));
97b8244 @matthewd Root everything in sight.
matthewd authored
411
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
412 *objp = obj;
413 }
97b8244 @matthewd Root everything in sight.
matthewd authored
414
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
415 JRETURN;
6b3683d @jbarnette Make with() work. [#31 state:resolved]
authored
416 }
417
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
418 static JSBool to_string(JSContext* js_context, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* retval)
419 {
420 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
421
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
422 JohnsonContext* context;
423 JohnsonRuntime* runtime;
424 Data_Get_Struct(ruby_context, JohnsonContext, context);
425
426 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
427 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
428
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
429 PREPARE_JROOTS(js_context, 0);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
430
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
431 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
432
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
433 JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("to_s"), 0));
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
434 JRETURN;
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
435 }
436
437 static JSBool to_array(JSContext* js_context, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* retval)
438 {
439 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
440
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
441 JohnsonContext* context;
442 JohnsonRuntime* runtime;
443 Data_Get_Struct(ruby_context, JohnsonContext, context);
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
444
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
445 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
446 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
447
448 PREPARE_JROOTS(js_context, 0);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
449
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
450 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
451
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
452 JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("to_a"), 0));
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
453 JRETURN;
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
454 }
455
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
456 static JSBool method_missing(JSContext* js_context, JSObject* obj, uintN argc, jsval* argv, jsval* retval)
457 {
97b8244 @matthewd Root everything in sight.
matthewd authored
458 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
459
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
460 JohnsonContext* context;
461 JohnsonRuntime* runtime;
462 Data_Get_Struct(ruby_context, JohnsonContext, context);
463
464 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
465 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
466
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
467 PREPARE_JROOTS(js_context, 0);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
468
97b8244 @matthewd Root everything in sight.
matthewd authored
469 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
470
97b8244 @matthewd Root everything in sight.
matthewd authored
471 assert(argc >= 2);
472
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
473 char* key = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
474 VALUE ruby_id = rb_intern(key);
475
78f8bf9 @jbarnette Naming and formatting nitpicks.
authored
476 // FIXME: this is horrible and lazy, to_a comes from enumerable on proxy (argv[1] is a JSArray)
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
477 VALUE args;
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
478 JCHECK(call_ruby_from_js2(runtime, &args, CONVERT_TO_RUBY(runtime, argv[1]), rb_intern("to_a"), 0));
c1a7fbf @matthewd Added a bunch of exception propagation handling.
matthewd authored
479
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
480 JCHECK(call_ruby_from_js(runtime, retval, Johnson_SpiderMonkey_JSLandProxy(),
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
481 rb_intern("send_with_possible_block"), 3, self, ID2SYM(ruby_id), args));
482
483 JRETURN;
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
484 }
485
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
486 static JSBool call(JSContext* js_context, JSObject* UNUSED(obj), uintN argc, jsval* argv, jsval* retval)
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
487 {
97b8244 @matthewd Root everything in sight.
matthewd authored
488 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
489
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
490 JohnsonContext* context;
491 JohnsonRuntime* runtime;
492 Data_Get_Struct(ruby_context, JohnsonContext, context);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
493
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
494 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
495 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
496
497 PREPARE_JROOTS(js_context, 0);
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
498
97b8244 @matthewd Root everything in sight.
matthewd authored
499 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, JSVAL_TO_OBJECT(JS_ARGV_CALLEE(argv)), &JSLandCallableProxyClass, NULL);
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
500
501 VALUE args = rb_ary_new();
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
502
503 uintN i;
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
504 for (i = 0; i < argc; ++i)
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
505 rb_ary_push(args, CONVERT_TO_RUBY(runtime, argv[i]));
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
506
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
507 JCHECK(call_ruby_from_js(runtime, retval, Johnson_SpiderMonkey_JSLandProxy(),
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
508 rb_intern("send_with_possible_block"), 3, self, ID2SYM(rb_intern("call")), args));
509 JRETURN;
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
510 }
511
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
512 bool js_value_is_proxy(JohnsonRuntime* MAYBE_UNUSED(runtime), jsval maybe_proxy)
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
513 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
514 JSClass* klass = JS_GET_CLASS(
515 johnson_get_current_context(runtime),
516 JSVAL_TO_OBJECT(maybe_proxy));
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
517
518 return &JSLandProxyClass == klass
519 || &JSLandClassProxyClass == klass
520 || &JSLandCallableProxyClass == klass;
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
521 }
522
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
523 VALUE unwrap_js_land_proxy(JohnsonRuntime* runtime, jsval proxy)
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
524 {
01b0682 @jbarnette JS proxies roundtrip.
authored
525 VALUE value;
109858e @jbarnette Make JSLandProxyClass more easily parameterizable.
authored
526 JSObject *proxy_object = JSVAL_TO_OBJECT(proxy);
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
527 JSContext * context = johnson_get_current_context(runtime);
109858e @jbarnette Make JSLandProxyClass more easily parameterizable.
authored
528
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
529 value = (VALUE)JS_GetInstancePrivate(context, proxy_object,
530 JS_GET_CLASS(context, proxy_object), NULL);
109858e @jbarnette Make JSLandProxyClass more easily parameterizable.
authored
531
f0cc336 js proxies roundtrip correctly. still no gc root.
jbarnette authored
532 return value;
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
533 }
534
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
535 static void finalize(JSContext* js_context, JSObject* obj)
536 {
537 VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
538
539 if (ruby_context)
540 {
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
541 JohnsonContext* context;
542 JohnsonRuntime* runtime;
543 Data_Get_Struct(ruby_context, JohnsonContext, context);
544
545 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
546 Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
547
97b8244 @matthewd Root everything in sight.
matthewd authored
548 VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj,
549 JS_GET_CLASS(context->js, obj), NULL);
0795f89 @matthewd Track gcthings on the runtime, not the context.
matthewd authored
550
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
551 // remove the proxy OID from the id map
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
552 JS_HashTableRemove(runtime->rbids, (void *)self);
a84310c @tenderlove removing gc thing by object id
tenderlove authored
553
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
554 // free up the ruby value for GC
a84310c @tenderlove removing gc thing by object id
tenderlove authored
555 rb_funcall(ruby_runtime, rb_intern("remove_gcthing"), 1, rb_obj_id(self));
556 }
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
557 }
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
558
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
559 JSBool make_js_land_proxy(JohnsonRuntime* runtime, VALUE value, jsval* retval)
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
560 {
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
561 *retval = (jsval)JS_HashTableLookup(runtime->rbids, (void *)value);
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
562
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
563 if (*retval)
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
564 {
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
565 return JS_TRUE;
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
566 }
567 else
568 {
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
569 JSContext * context = johnson_get_current_context(runtime);
2b17b4d @matthewd Simplified the PREPARE_JROOTS API back to just one number.
matthewd authored
570 PREPARE_JROOTS(context, 1);
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
571
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
572 JSObject *jsobj;
b77b3ad @jbarnette Make sure multiple proxy classes are supported.
authored
573
574 JSClass *klass = &JSLandProxyClass;
575 if (T_CLASS == TYPE(value)) klass = &JSLandClassProxyClass;
576
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
577 // FIXME: hack; should happen in Rubyland
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
578 if (T_STRUCT == TYPE(value))
579 rb_funcall(Johnson_SpiderMonkey_JSLandProxy(),
580 rb_intern("treat_all_properties_as_methods"), 1, value);
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
581
f518879 @jbarnette Any Rubyland object that responds to call can be called as a function in...
authored
582 bool callable_p = Qtrue == rb_funcall(value,
583 rb_intern("respond_to?"), 1, rb_str_new2("call"));
b7616dd @jbarnette Reimplement property and method access. Mostly.
authored
584
73ad637 @jbarnette Cleaned up proxying of callable (Proc, Method) Ruby objects.
authored
585 if (callable_p)
586 klass = &JSLandCallableProxyClass;
587
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
588 JCHECK((jsobj = JS_NewObject(context, klass, NULL, NULL)));
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
589 JROOT(jsobj);
f518879 @jbarnette Any Rubyland object that responds to call can be called as a function in...
authored
590
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
591 JCHECK(JS_SetPrivate(context, jsobj, (void*)value));
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
592
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
593 JCHECK(JS_DefineFunction(context, jsobj, "__noSuchMethod__", method_missing, 2, 0));
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
594
ae3584f @jbarnette Runtime is the new Context. One context per Ruby thread.
authored
595 JCHECK(JS_DefineFunction(context, jsobj, "toArray", to_array, 0, 0));
596 JCHECK(JS_DefineFunction(context, jsobj, "toString", to_string, 0, 0));
70adb2d @matthewd Enabled a bunch of warnings, and added .toString/.toArray/.to_s
matthewd authored
597
9149af0 @matthewd More JSBool conversions.
matthewd authored
598 *retval = OBJECT_TO_JSVAL(jsobj);
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
599
600 // put the proxy OID in the id map
c6ea599 @matthewd Use direct pointers in the hash tables etc, and just Root values we need...
matthewd authored
601 JCHECK(JS_HashTableAdd(runtime->rbids, (void *)value, (void *)(*retval)));
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
602
603 // root the ruby value for GC
0795f89 @matthewd Track gcthings on the runtime, not the context.
matthewd authored
604 VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(runtime->js);
605 rb_funcall(ruby_runtime, rb_intern("add_gcthing"), 1, value);
9149af0 @matthewd More JSBool conversions.
matthewd authored
606
06cd4a9 @matthewd Use some macros to keep track of what's been rooted.
matthewd authored
607 JRETURN;
3c24fe9 Fixed Ruby value tracking/GC rooting.
jbarnette authored
608 }
69ace87 JS-land proxies for Ruby objects. No support for GC or roundtripping.
jbarnette authored
609 }
Something went wrong with that request. Please try again.