public
Description: Johnson wraps JavaScript in a loving Ruby embrace.
Homepage: http://github.com/jbarnette/johnson/wikis
Clone URL: git://github.com/jbarnette/johnson.git
johnson / ext / spidermonkey / ruby_land_proxy.c
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 1 #include "ruby_land_proxy.h"
dc423367 » matthewd 2008-04-21 Deal with a couple of minor... 2 #include "conversions.h"
3 #include "error.h"
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 4
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 5 DECLARE_RUBY_WRAPPER(rb_call_super, int argc; const VALUE* argv)
6 DEFINE_RUBY_WRAPPER(rb_call_super, rb_call_super, ARGLIST2(argc, argv))
7
8 DECLARE_RUBY_WRAPPER(rb_yield, VALUE v)
9 DEFINE_RUBY_WRAPPER(rb_yield, rb_yield, ARGLIST1(v))
10
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 11 static VALUE proxy_class = Qnil;
12
df61324f » matthewd 2008-05-23 assert() is only to catch i... 13 static JSBool get_jsval_for_proxy(RubyLandProxy* proxy, jsval* jv)
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 14 {
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 15 JSContext * context = johnson_get_current_context(proxy->runtime);
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 16 PREPARE_JROOTS(context, 0);
df61324f » matthewd 2008-05-23 assert() is only to catch i... 17
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 18 // FIXME: this is totally lame
19 char global_key[10];
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 20 sprintf(global_key, "%x", (int)proxy->runtime->global);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 21
22 if (0 == strcmp(global_key, proxy->key))
df61324f » matthewd 2008-05-23 assert() is only to catch i... 23 {
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 24 *jv = OBJECT_TO_JSVAL(proxy->runtime->global);
df61324f » matthewd 2008-05-23 assert() is only to catch i... 25 JRETURN;
26 }
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 27
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 28 JCHECK(JS_GetProperty(context, proxy->runtime->gcthings, proxy->key, jv));
df61324f » matthewd 2008-05-23 assert() is only to catch i... 29 JRETURN;
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 30 }
31
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 32 static VALUE call_js_function_value(JohnsonRuntime* runtime, jsval target, jsval function, int argc, VALUE* argv)
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 33 {
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 34 JSContext * context = johnson_get_current_context(runtime);
2b17b4dd » matthewd 2008-05-04 Simplified the PREPARE_JROO... 35 PREPARE_RUBY_JROOTS(context, argc + 2);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 36
37 JROOT(target);
38 JROOT(function);
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 39
40 assert(JSVAL_IS_OBJECT(target));
41
42 jsval args[argc];
43 jsval result;
44
45 int i;
46 for(i = 0; i < argc; ++i)
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 47 {
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 48 JCHECK(convert_to_js(runtime, argv[i], &(args[i])));
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 49 JROOT(args[i]);
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 50 }
51
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 52 JCHECK(JS_CallFunctionValue(context,
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 53 JSVAL_TO_OBJECT(target), function, (unsigned) argc, args, &result));
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 54
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 55 JRETURN_RUBY(CONVERT_TO_RUBY(runtime, result));
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 56 }
57
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 58 /*
59 * call-seq:
60 * [](name)
61 *
62 * Returns the property with +name+.
63 */
64 static VALUE
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 65 get(VALUE self, VALUE name)
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 66 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 67 RubyLandProxy* proxy;
68 Data_Get_Struct(self, RubyLandProxy, proxy);
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 69
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 70 JSContext * context = johnson_get_current_context(proxy->runtime);
71 PREPARE_RUBY_JROOTS(context, 1);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 72
df61324f » matthewd 2008-05-23 assert() is only to catch i... 73 jsval proxy_value;
74 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 75 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 76
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 77 jsval js_value;
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 78
79 switch(TYPE(name)) {
80 case T_FIXNUM:
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 81 JCHECK(JS_GetElement(context,
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 82 JSVAL_TO_OBJECT(proxy_value), NUM2INT(name), &js_value));
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 83 break;
84 default:
85 Check_Type(name, T_STRING);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 86 JCHECK(JS_GetProperty(context,
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 87 JSVAL_TO_OBJECT(proxy_value), StringValueCStr(name), &js_value));
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 88 break;
89 }
90
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 91 JRETURN_RUBY(CONVERT_TO_RUBY(proxy->runtime, js_value));
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 92 }
93
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 94 /*
95 * call-seq:
96 * []=(name,value)
97 *
98 * Sets the property with +name+ to +value+.
99 */
100 static VALUE
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 101 set(VALUE self, VALUE name, VALUE value)
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 102 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 103 RubyLandProxy* proxy;
104 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 105 JSContext * context = johnson_get_current_context(proxy->runtime);
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 106
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 107 PREPARE_RUBY_JROOTS(context, 2);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 108
df61324f » matthewd 2008-05-23 assert() is only to catch i... 109 jsval proxy_value;
110 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 111 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 112
a5d745ef » matthewd 2008-04-21 Make convert_to_js() return... 113 jsval js_value;
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 114 JCHECK(convert_to_js(proxy->runtime, value, &js_value));
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 115
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 116 JROOT(js_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 117
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 118 switch(TYPE(name)) {
119 case T_FIXNUM:
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 120 JCHECK(JS_SetElement(context,
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 121 JSVAL_TO_OBJECT(proxy_value), NUM2INT(name), &js_value));
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 122 break;
123 default:
124 Check_Type(name, T_STRING);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 125 JCHECK(JS_SetProperty(context,
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 126 JSVAL_TO_OBJECT(proxy_value), StringValueCStr(name), &js_value));
4867becd » tenderlove 2008-04-17 #30 making arrays indexable... 127 break;
128 }
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 129
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 130 JRETURN_RUBY(value);
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 131 }
132
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 133 /*
134 * call-seq:
135 * function?
136 *
137 * Returns <code>true</code> if this is a function.
138 */
139 static VALUE
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 140 function_p(VALUE self)
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 141 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 142 RubyLandProxy* proxy;
143 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 144 JSContext * context = johnson_get_current_context(proxy->runtime);
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 145 PREPARE_RUBY_JROOTS(context, 0);
df61324f » matthewd 2008-05-23 assert() is only to catch i... 146 jsval proxy_value;
147 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 148 JRETURN_RUBY(JS_TypeOfValue(context, proxy_value) == JSTYPE_FUNCTION ? Qtrue : Qfalse);
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 149 }
150
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 151 /*
152 * call-seq:
153 * respond_to?(symbol)
154 *
155 * Returns <code>true</code> if _obj_ responds to given method.
156 */
157 static VALUE
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 158 respond_to_p(VALUE self, VALUE sym)
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 159 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 160 RubyLandProxy* proxy;
161 Data_Get_Struct(self, RubyLandProxy, proxy);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 162
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 163 JSContext * context = johnson_get_current_context(proxy->runtime);
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 164 PREPARE_RUBY_JROOTS(context, 2);
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 165
166 char* name = rb_id2name(SYM2ID(sym));
167
168 // assignment is always okay
169 if (name[strlen(name) - 1] == '=')
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 170 JRETURN_RUBY(Qtrue);
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 171
df61324f » matthewd 2008-05-23 assert() is only to catch i... 172 jsval proxy_value;
173 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 174 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 175
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 176 JSObject *obj;
177 JSBool found;
178
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 179 JCHECK(JS_ValueToObject(context, proxy_value, &obj));
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 180 JROOT(obj);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 181
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 182 JCHECK(JS_HasProperty(context, obj, name, &found));
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 183
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 184 JRETURN_RUBY(found ? Qtrue : CALL_RUBY_WRAPPER(rb_call_super, 1, &sym));
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 185 }
186
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 187 /*
188 * call-seq:
189 * native_call(global, *args)
190 *
191 * Call as a function with given +global+ using *args.
192 */
193 static VALUE
b7616ddc » jbarnette 2008-04-20 Reimplement property and me... 194 native_call(int argc, VALUE* argv, VALUE self)
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 195 {
196 if (!function_p(self))
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 197 Johnson_Error_raise("This Johnson::SpiderMonkey::RubyLandProxy isn't a function.");
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 198
b05fc61d » matthewd 2008-04-25 Check argc to ensure we are... 199 if (argc < 1)
200 rb_raise(rb_eArgError, "Target object required");
201
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 202 RubyLandProxy* proxy;
203 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 204 JSContext * context = johnson_get_current_context(proxy->runtime);
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 205
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 206 PREPARE_RUBY_JROOTS(context, 1);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 207
df61324f » matthewd 2008-05-23 assert() is only to catch i... 208 jsval proxy_value;
209 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 210 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 211
a5d745ef » matthewd 2008-04-21 Make convert_to_js() return... 212 jsval global;
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 213 JCHECK(convert_to_js(proxy->runtime, argv[0], &global));
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 214
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 215 JRETURN_RUBY(call_js_function_value(proxy->runtime, global, proxy_value, argc - 1, &(argv[1])));
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 216 }
217
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 218 static void
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 219 destroy_id_array(JSContext* context, void* data)
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 220 {
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 221 JS_DestroyIdArray(context, (JSIdArray*)data);
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 222 }
223
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 224 /*
225 * call-seq:
226 * each { |obj| block }
227 *
228 * Calls <em>block</em> with each item in the collection.
229 */
230 static VALUE
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 231 each(VALUE self)
232 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 233 RubyLandProxy* proxy;
234 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 235 JSContext * context = johnson_get_current_context(proxy->runtime);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 236
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 237 PREPARE_RUBY_JROOTS(context, 5);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 238
df61324f » matthewd 2008-05-23 assert() is only to catch i... 239 jsval proxy_value;
240 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 241 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 242
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 243 JSObject* value = JSVAL_TO_OBJECT(proxy_value);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 244 JROOT(value);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 245
246 // arrays behave like you'd expect, indexes in order
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 247 if (JS_IsArrayObject(context, value))
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 248 {
249 jsuint length;
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 250 JCHECK(JS_GetArrayLength(context, value, &length));
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 251
70adb2d1 » matthewd 2008-04-25 Enabled a bunch of warnings... 252 jsuint i = 0;
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 253 for (i = 0; i < length; ++i)
254 {
255 jsval element;
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 256 JCHECK(JS_GetElement(context, value, (signed) i, &element));
257 CALL_RUBY_WRAPPER(rb_yield, convert_to_ruby(proxy->runtime, element));
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 258 }
259 }
260 else
261 {
262 // not an array? behave like each on Hash; yield [key, value]
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 263 JSIdArray* ids = JS_Enumerate(context, value);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 264 JCHECK(ids);
265
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 266 JCLEANUP(destroy_id_array, ids);
ce00ab69 » matthewd 2008-04-28 Cautiously edge toward a sl... 267
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 268 int i;
aa63d637 » jbarnette 2008-03-19 Some Enumerable-related FIX... 269 for (i = 0; i < ids->length; ++i)
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 270 {
271 jsval js_key, js_value;
272
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 273 JCHECK(JS_IdToValue(context, ids->vector[i], &js_key));
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 274 JROOT(js_key);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 275
276 if (JSVAL_IS_STRING(js_key))
277 {
278 // regular properties have string keys
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 279 JCHECK(JS_GetProperty(context, value,
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 280 JS_GetStringBytes(JSVAL_TO_STRING(js_key)), &js_value));
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 281 }
282 else
283 {
284 // it's a numeric property, use array access
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 285 JCHECK(JS_GetElement(context, value,
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 286 JSVAL_TO_INT(js_key), &js_value));
c1a7fbfe » matthewd 2008-04-24 Added a bunch of exception ... 287 }
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 288 JROOT(js_value);
c1a7fbfe » matthewd 2008-04-24 Added a bunch of exception ... 289
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 290 VALUE key = CONVERT_TO_RUBY(proxy->runtime, js_key);
291 VALUE value = CONVERT_TO_RUBY(proxy->runtime, js_value);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 292
eb42dce4 » matthewd 2008-04-29 New plan: let's not use nes... 293 CALL_RUBY_WRAPPER(rb_yield, rb_ary_new3(2, key, value));
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 294
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 295 JUNROOT(js_value);
296 JUNROOT(js_key);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 297 }
298 }
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 299
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 300 JRETURN_RUBY(self);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 301 }
302
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 303 /*
304 * call-seq:
305 * length
306 *
307 * Returns the length of the collection.
308 */
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 309 static VALUE
310 length(VALUE self)
311 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 312 RubyLandProxy* proxy;
313 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 314 JSContext * context = johnson_get_current_context(proxy->runtime);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 315
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 316 PREPARE_RUBY_JROOTS(context, 2);
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 317
df61324f » matthewd 2008-05-23 assert() is only to catch i... 318 jsval proxy_value;
319 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 320 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 321
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 322 JSObject* value = JSVAL_TO_OBJECT(proxy_value);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 323 JROOT(value);
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 324
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 325 if (JS_IsArrayObject(context, value))
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 326 {
327 jsuint length;
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 328 JCHECK(JS_GetArrayLength(context, value, &length));
c1a7fbfe » matthewd 2008-04-24 Added a bunch of exception ... 329
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 330 JRETURN_RUBY(INT2FIX(length));
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 331 }
332 else
333 {
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 334 JSIdArray* ids = JS_Enumerate(context, value);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 335 JCHECK(ids);
336 VALUE length = INT2FIX(ids->length);
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 337
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 338 JS_DestroyIdArray(context, ids);
c1a7fbfe » matthewd 2008-04-24 Added a bunch of exception ... 339
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 340 JRETURN_RUBY(length);
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 341 }
342 }
343
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 344 /*
345 * call-seq:
dc2a8331 » tenderlove 2008-05-30 making more shit work 346 * runtime
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 347 *
dc2a8331 » tenderlove 2008-05-30 making more shit work 348 * Returns runtime.
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 349 */
350 static VALUE
dc2a8331 » tenderlove 2008-05-30 making more shit work 351 runtime(VALUE self)
7084c002 » tenderlove 2008-04-09 extracting global object fo... 352 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 353 RubyLandProxy* proxy;
354 Data_Get_Struct(self, RubyLandProxy, proxy);
dc2a8331 » tenderlove 2008-05-30 making more shit work 355 return (VALUE)JS_GetRuntimePrivate(proxy->runtime->js);
7084c002 » tenderlove 2008-04-09 extracting global object fo... 356 }
357
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 358 /*
359 * call-seq:
360 * function_property?(name)
361 *
362 * Returns <code>true</code> if +name+ is a function property.
363 */
364 static VALUE
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 365 function_property_p(VALUE self, VALUE name)
366 {
367 Check_Type(name, T_STRING);
368
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 369 RubyLandProxy* proxy;
370 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 371 JSContext * context = johnson_get_current_context(proxy->runtime);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 372
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 373 PREPARE_RUBY_JROOTS(context, 2);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 374
df61324f » matthewd 2008-05-23 assert() is only to catch i... 375 jsval proxy_value;
376 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 377 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 378
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 379 jsval js_value;
380
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 381 JCHECK(JS_GetProperty(context,
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 382 JSVAL_TO_OBJECT(proxy_value), StringValueCStr(name), &js_value));
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 383
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 384 JROOT(js_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 385
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 386 JSType type = JS_TypeOfValue(context, js_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 387
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 388 JRETURN_RUBY(type == JSTYPE_FUNCTION ? Qtrue : Qfalse);
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 389 }
390
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 391 /*
392 * call-seq:
393 * call_function_property(name, arguments)
394 *
395 * Calls function +name+ with +arguments+.
396 */
397 static VALUE
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 398 call_function_property(int argc, VALUE* argv, VALUE self)
399 {
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 400 RubyLandProxy* proxy;
401 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 402 JSContext * context = johnson_get_current_context(proxy->runtime);
b05fc61d » matthewd 2008-04-25 Check argc to ensure we are... 403
404 if (argc < 1)
405 rb_raise(rb_eArgError, "Function name required");
406
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 407 PREPARE_RUBY_JROOTS(context, 2);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 408
df61324f » matthewd 2008-05-23 assert() is only to catch i... 409 jsval proxy_value;
410 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 411 JROOT(proxy_value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 412
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 413 jsval function;
414
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 415 JCHECK(JS_GetProperty(context,
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 416 JSVAL_TO_OBJECT(proxy_value), StringValueCStr(argv[0]), &function));
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 417
418 JROOT(function);
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 419
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 420 JSType funtype = JS_TypeOfValue(context, function);
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 421
422 // should never be anything but a function
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 423 if (funtype != JSTYPE_FUNCTION)
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 424 JERROR("Specified property \"%s\" isn't a function.", StringValueCStr(argv[0]));
3b26152f » matthewd 2008-04-26 Fix call_function_property,... 425
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 426 JRETURN_RUBY(call_js_function_value(proxy->runtime, proxy_value, function, argc - 1, &(argv[1])));
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 427 }
428
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 429 /*
430 * call-seq:
431 * to_s
432 *
433 * Converts object to a string.
434 */
435 static VALUE to_s(VALUE self)
436 {
437 RubyLandProxy* proxy;
438 Data_Get_Struct(self, RubyLandProxy, proxy);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 439 JSContext * context = johnson_get_current_context(proxy->runtime);
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 440
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 441 PREPARE_RUBY_JROOTS(context, 1);
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 442
df61324f » matthewd 2008-05-23 assert() is only to catch i... 443 jsval proxy_value;
444 JCHECK(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 445 JROOT(proxy_value);
446
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 447 JSString* str = JS_ValueToString(context, proxy_value);
448 JRETURN_RUBY(convert_js_string_to_ruby(proxy->runtime, str));
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 449 }
450
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 451 ///////////////////////////////////////////////////////////////////////////
452 //// INFRASTRUCTURE BELOW HERE ////////////////////////////////////////////
453 ///////////////////////////////////////////////////////////////////////////
454
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 455 static void finalize(RubyLandProxy* proxy)
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 456 {
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 457 JSContext * context = johnson_get_current_context(proxy->runtime);
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 458 PREPARE_RUBY_JROOTS(context, 0);
df61324f » matthewd 2008-05-23 assert() is only to catch i... 459 jsval proxy_value;
460 JCHECK_RUBY(get_jsval_for_proxy(proxy, &proxy_value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 461
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 462 // could get finalized after the context has been freed
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 463 if (proxy->runtime && proxy->runtime->jsids)
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 464 {
465 // remove this proxy from the OID map
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 466 JS_HashTableRemove(proxy->runtime->jsids, (void *)proxy_value);
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 467
468 // remove our GC handle on the JS value
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 469 JS_DeleteProperty(context, proxy->runtime->gcthings, proxy->key);
f8a92fab » jbarnette 2008-03-17 Beating down some tramply p... 470
6a981ce4 » tenderlove 2008-05-30 converting rubylandproxy 471 proxy->runtime = 0;
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 472 }
473
474 free(proxy);
475 }
476
0746d926 » matthewd 2008-04-21 Use C99 bool for simple con... 477 bool ruby_value_is_proxy(VALUE maybe_proxy)
89d849d3 » jbarnette 2008-03-23 Proxies get unwrapped corre... 478 {
479 return proxy_class == CLASS_OF(maybe_proxy);
480 }
481
0704a464 » tenderlove 2008-05-30 converting conversions to J... 482 JSBool unwrap_ruby_land_proxy(JohnsonRuntime* runtime, VALUE wrapped, jsval* retval)
89d849d3 » jbarnette 2008-03-23 Proxies get unwrapped corre... 483 {
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 484 JSContext * context = johnson_get_current_context(runtime);
89d849d3 » jbarnette 2008-03-23 Proxies get unwrapped corre... 485 assert(ruby_value_is_proxy(wrapped));
486
df61324f » matthewd 2008-05-23 assert() is only to catch i... 487 PREPARE_JROOTS(context, 0);
488
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 489 RubyLandProxy* proxy;
490 Data_Get_Struct(wrapped, RubyLandProxy, proxy);
89d849d3 » jbarnette 2008-03-23 Proxies get unwrapped corre... 491
df61324f » matthewd 2008-05-23 assert() is only to catch i... 492 JCHECK(get_jsval_for_proxy(proxy, retval));
493 JRETURN;
89d849d3 » jbarnette 2008-03-23 Proxies get unwrapped corre... 494 }
495
3680b337 » tenderlove 2008-05-30 incomplete converting a bun... 496 VALUE make_ruby_land_proxy(JohnsonRuntime* runtime, jsval value)
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 497 {
3680b337 » tenderlove 2008-05-30 incomplete converting a bun... 498 VALUE id = (VALUE)JS_HashTableLookup(runtime->jsids, (void *)value);
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 499 JSContext * context = johnson_get_current_context(runtime);
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 500
501 if (id)
502 {
503 // if we already have a proxy, return it
504 return rb_funcall(rb_const_get(rb_cObject,
505 rb_intern("ObjectSpace")), rb_intern("_id2ref"), 1, id);
506 }
507 else
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 508 {
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 509 // otherwise make one and cache it
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 510 RubyLandProxy* our_proxy;
511 VALUE proxy = Data_Make_Struct(proxy_class, RubyLandProxy, 0, finalize, our_proxy);
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 512
2b17b4dd » matthewd 2008-05-04 Simplified the PREPARE_JROO... 513 PREPARE_RUBY_JROOTS(context, 1);
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 514 JROOT(value);
97b82442 » matthewd 2008-04-21 Root everything in sight. Comment 515
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 516 // root the value for JS GC and lookups
517 sprintf(our_proxy->key, "%x", (int)value);
518
c7079b18 » tenderlove 2008-05-30 got ruby land proxy to compile 519 JCHECK(JS_SetProperty(context, runtime->gcthings, our_proxy->key, &value));
f21981e9 » jbarnette 2008-05-20 RubyLandProxies store an op... 520
3680b337 » tenderlove 2008-05-30 incomplete converting a bun... 521 our_proxy->runtime = runtime;
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 522
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 523 // put the proxy OID in the id map
3680b337 » tenderlove 2008-05-30 incomplete converting a bun... 524 JCHECK(JS_HashTableAdd(runtime->jsids, (void *)value, (void *)rb_obj_id(proxy)));
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 525
06cd4a94 » matthewd 2008-04-27 Use some macros to keep tra... 526 JRETURN_RUBY(proxy);
3c24fe99 » jbarnette 2008-04-01 Fixed Ruby value tracking/G... 527 }
528 }
529
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 530 void init_Johnson_SpiderMonkey_Proxy(VALUE spidermonkey)
531 {
2fc27423 » tenderlove 2008-05-09 Added RDoc to the native code. 532 /* HACK: These comments are *only* to make RDoc happy.
533 VALUE johnson = rb_define_module("Johnson");
534 VALUE spidermonkey = rb_define_module_under(johnson, "SpiderMonkey");
535 */
536
537 /* RubyLandProxy class. */
e18f74d8 » jbarnette 2008-04-14 Renaming proxies for clarity. Comment 538 proxy_class = rb_define_class_under(spidermonkey, "RubyLandProxy", rb_cObject);
a2a230ef » jbarnette 2008-03-17 Beginnings of method suppor... 539
82b9c932 » jbarnette 2008-03-17 Simple proxy indexing works. 540 rb_define_method(proxy_class, "[]", get, 1);
a2a230ef » jbarnette 2008-03-17 Beginnings of method suppor... 541 rb_define_method(proxy_class, "[]=", set, 2);
2660dba4 » jbarnette 2008-03-18 Simple Ruby->JS proxy funca... 542 rb_define_method(proxy_class, "function?", function_p, 0);
9d68f23b » jbarnette 2008-03-18 Proxy#respond_to? 543 rb_define_method(proxy_class, "respond_to?", respond_to_p, 1);
f062b403 » jbarnette 2008-03-19 Proxies are enumerable. 544 rb_define_method(proxy_class, "each", each, 0);
95108d5a » jbarnette 2008-03-19 length/size work on proxies. 545 rb_define_method(proxy_class, "length", length, 0);
70adb2d1 » matthewd 2008-04-25 Enabled a bunch of warnings... 546 rb_define_method(proxy_class, "to_s", to_s, 0);
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 547
7084c002 » tenderlove 2008-04-09 extracting global object fo... 548 rb_define_private_method(proxy_class, "native_call", native_call, -1);
dc2a8331 » tenderlove 2008-05-30 making more shit work 549 rb_define_private_method(proxy_class, "runtime", runtime, 0);
dfbd5bda » jbarnette 2008-03-18 JS functions work in Rubyland. 550 rb_define_private_method(proxy_class, "function_property?", function_property_p, 1);
551 rb_define_private_method(proxy_class, "call_function_property", call_function_property, -1);
fe394493 » jbarnette 2008-03-17 Rubyland proxies aren't any... 552 }