Skip to content
Newer
Older
100644 1900 lines (1636 sloc) 58.5 KB
af5695f import from tar
nazoking authored Oct 3, 2006
1 #define JS_C_STRINGS_ARE_UTF8
391f558 * Change : pkg-config を使うように変更( kou )
nazoking authored Oct 8, 2006
2
3 #include <ruby.h>
4
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
5 //#define DEBUG
6
0aec8ac @matthewd Allow use of mozjs/ instead of smjs/.
matthewd authored Jul 24, 2007
7 #if defined NEED_MOZJS_PREFIX
8 # include <mozjs/jsapi.h>
9 # include <mozjs/jshash.h>
10 # include <mozjs/jsobj.h>
11 #elif defined NEED_SMJS_PREFIX
391f558 * Change : pkg-config を使うように変更( kou )
nazoking authored Oct 8, 2006
12 # include <smjs/jsapi.h>
13 # include <smjs/jshash.h>
14 # include <smjs/jsobj.h>
15 #else
16 # include <jsapi.h>
17 # include <jshash.h>
18 # include <jsobj.h>
19 #endif
af5695f import from tar
nazoking authored Oct 3, 2006
20
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
21 // デフォルトのスタックサイズ : Default stack size
b56b341 @matthewd Corrected typo in constant name.
matthewd authored Jan 20, 2008
22 #define JS_STACK_CHUNK_SIZE 16384
23
24 // SETTING THIS TOO LOW RESULTS IN SEGFAULTS!
02b416c @matthewd More attempts to get exception handling to behave as I want it to, and
matthewd authored Jul 28, 2007
25 // More specifically, having the runtime actually reach its maximum
26 // memory allocation appears to cause segfaults (JS_NewObject returns a
27 // bad pointer, then JS_DefineFunction segfaults). It is thus quite
28 // important to call JS_GC frequently... we call JS_MaybeGC before
29 // evaluating a script to reduce the problem.
b56b341 @matthewd Corrected typo in constant name.
matthewd authored Jan 20, 2008
30 #define JS_RUNTIME_MAXBYTES 0x3000000L
af5695f import from tar
nazoking authored Oct 3, 2006
31
32 #define RBSMJS_DEFAULT_CONTEXT "@@defaultContext"
33 #define RBSMJS_VALUES_CONTEXT "@context"
34 #define RBSMJS_CONTEXT_GLOBAL "global"
35 #define RBSMJS_CONTEXT_BINDINGS "bindings"
36
37 #define RBSM_CONVERT_SHALLOW 1
38 #define RBSM_CONVERT_DEEP 0
39
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
40
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
41 #ifdef WIN32
42 # ifdef DEBUG
43 # define trace printf("\n"); printf
44 # else
45 # define trace(msg)
46 # endif
666cebb @matthewd Prevent ugly failures when DEBUG is undefined (rather than set to a
matthewd authored Jan 20, 2008
47 #elif defined DEBUG
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
48 # define trace(format, ...) do { printf(format, ## __VA_ARGS__); printf("\n"); } while (0)
49 #else
50 # define trace(format, ...)
51 #endif
52
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
53
54 #define ZERO_ARITY_METHOD_IS_PROPERTY
55
455d700 @matthewd Natively convert Ruby Time and Date (and, through inheritance, DateTime)
matthewd authored Jan 20, 2008
56 VALUE rb_cDate;
57
af5695f import from tar
nazoking authored Oct 3, 2006
58 VALUE eJSError;
59 VALUE eJSConvertError;
60 VALUE eJSEvalError;
61 VALUE cJSValue;
62 VALUE cJSContext;
63 VALUE cJSFunction;
64 VALUE cSMJS;
65 JSRuntime* gSMJS_runtime;
66
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
67 #ifdef DEBUG
68 int alloc_count_js2rb;
69 int alloc_count_rb2js;
70 #endif
71
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
72 // RubyObject/RubyFunction が持つ情報 : Properties
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
73 typedef struct{
af5695f import from tar
nazoking authored Oct 3, 2006
74 VALUE rbobj;
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
75 jsval jsv;
af5695f import from tar
nazoking authored Oct 3, 2006
76 }sSMJS_Class;
77
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
78 // RubyException エラー : Error
af5695f import from tar
nazoking authored Oct 3, 2006
79 typedef struct{
80 int status;
81 jsval erval;
82 VALUE errinfo;
83 }sSMJS_Error;
84
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
85 // SpiderMonkey::Context -- インスタンスが持つ情報を格納した構造体
86 // -- Structure containing instance data
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
87 typedef struct{
af5695f import from tar
nazoking authored Oct 3, 2006
88 JSContext* cx;
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
89 jsval last_exception; // 最後のJS例外 : Last JS Exception
90 char last_message[BUFSIZ]; // 最後のエラーメッセージ : Last Error Message
91 JSObject* store; // 対JS-GC用store : "For opposite JS-GC store"
92 JSHashTable* id2rbval; // JS-VALUE値とRuby-Object#__id__の対 : "JS-VALUE value and Ruby-Object#__id__[no] opposite"
af5695f import from tar
nazoking authored Oct 3, 2006
93 }sSMJS_Context;
94
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
95 // SpiderMonkey::Value -- インスタンスが持つ情報を格納した構造体
96 // -- Structure containing instance data
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
97 typedef struct{
af5695f import from tar
nazoking authored Oct 3, 2006
98 jsval value;
99 sSMJS_Context* cs;
100 }sSMJS_Value;
101
102 // each 用に必要な情報を持つ構造体
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
103 // "The structure which has the information which is necessary for business"
af5695f import from tar
nazoking authored Oct 3, 2006
104 typedef struct{
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
105 JSContext* cx;
af5695f import from tar
nazoking authored Oct 3, 2006
106 JSObject* obj;
107 jsint id;
108 jsval val;
109 jsval key;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
110 JSIdArray* ida;
af5695f import from tar
nazoking authored Oct 3, 2006
111 int i;
112 void* data;
113 char* keystr;
114 }sSMJS_Enumdata;
115
116 typedef void(* RBSMJS_YIELD)( sSMJS_Enumdata* enm );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
117 typedef VALUE(* RBSMJS_Convert)( JSContext* cx, jsval val );
118
119 static VALUE RBSMContext_FROM_JsContext( JSContext* cx );
120 static JSBool rbsm_class_get_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp );
121 static JSBool rbsm_class_set_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp );
122 static JSBool rbsm_error_get_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp );
123 static void rbsm_class_finalize ( JSContext* cx, JSObject* obj );
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
124 static JSBool rb_smjs_value_object_callback( JSContext* cx, JSObject* thisobj, uintN argc, jsval* argv, jsval* rval );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
125 static JSBool rb_smjs_value_function_callback( JSContext* cx, JSObject* thisobj, uintN argc, jsval* argv, jsval* rval );
126 static JSObject* rbsm_proc_to_function( JSContext* cx, VALUE proc );
127 static void rbsm_rberror_finalize ( JSContext* cx, JSObject* obj );
af5695f import from tar
nazoking authored Oct 3, 2006
128 static JSObject* rbsm_ruby_to_jsobject( JSContext* cx, VALUE obj );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
129 static JSBool rbsm_rubyexception_to_string( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval );
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
130 static JSBool rbsm_class_to_string( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval );
131 static JSBool rbsm_class_no_such_method( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
132 static VALUE rb_smjs_convertvalue( JSContext* cx, jsval value );
133 static VALUE rb_smjs_convert_prim( JSContext* cx, jsval value );
134 static JSBool rbsm_rubystring_to_jsval( JSContext* cx, VALUE rval, jsval* jval );
135 static JSBool rbsm_get_ruby_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp, VALUE rbobj );
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
136 static VALUE rbsm_evalerror_new( VALUE context, VALUE erval );
af5695f import from tar
nazoking authored Oct 3, 2006
137 static VALUE rbsm_evalerror_new_jsval( VALUE context, jsval jsvalerror );
138 static VALUE rb_smjs_value_new_jsval( VALUE context, jsval value );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
139 static VALUE rb_smjs_value_get_prototype( VALUE self );
140 static void* rbsm_each( JSContext* cx, jsval value, RBSMJS_YIELD yield, void* data );
141 static JSObjectOps* rbsm_class_get_object_ops( JSContext* cx, JSClass* clasp );
142 static void rb_smjs_context_errorhandle( JSContext* cx, const char* message, JSErrorReport* report );
96aef63 @matthewd Split out Context#flush, which resets the contents of the JS global
matthewd authored Jan 20, 2008
143 static VALUE rb_smjs_context_flush( VALUE self );
af5695f import from tar
nazoking authored Oct 3, 2006
144
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
145 typedef enum RBSMErrNum{
146 #define MSG_DEF( name, number, count, exception, format ) \
af5695f import from tar
nazoking authored Oct 3, 2006
147 name = number,
148 #include "rbsm.msg"
149 #undef MSG_DEF
150 RBSMErr_Limit
151 #undef MSGDEF
152 } RBSMErrNum;
153
154 JSErrorFormatString rbsm_ErrorFormatString[RBSMErr_Limit] = {
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
155 #define MSG_DEF( name, number, count, exception, format ) { format, count },
af5695f import from tar
nazoking authored Oct 3, 2006
156 #include "rbsm.msg"
157 #undef MSG_DEF
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
158 {0, 0}
af5695f import from tar
nazoking authored Oct 3, 2006
159 };
160
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
161 #ifndef JSCLASS_GLOBAL_FLAGS
162 #define JSCLASS_GLOBAL_FLAGS 0
163 #endif
164
af5695f import from tar
nazoking authored Oct 3, 2006
165 static JSClass global_class = {
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
166 "global", JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
af5695f import from tar
nazoking authored Oct 3, 2006
167 JS_PropertyStub, JS_PropertyStub,
168 JS_PropertyStub, JS_PropertyStub,
169 JS_EnumerateStub, JS_ResolveStub,
170 JS_ConvertStub, JS_FinalizeStub,
171 JSCLASS_NO_OPTIONAL_MEMBERS
172 };
173
174 static JSClass JSRubyObjectClass = {
175 "RubyObject", JSCLASS_HAS_PRIVATE,
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
176 /*addp*/JS_PropertyStub, /*delpr*/JS_PropertyStub,
177 /*getp*/rbsm_class_get_property, /*setp*/rbsm_class_set_property,
af5695f import from tar
nazoking authored Oct 3, 2006
178 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, rbsm_class_finalize,
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
179 /* getObjectOps */NULL, /*checkAccess */NULL, /*call*/rb_smjs_value_object_callback,
180 /* construct*/NULL, /* xdrObject*/NULL, /* hasInstance*/NULL, /* mark*/NULL,
181 /* spare*/0
af5695f import from tar
nazoking authored Oct 3, 2006
182 };
183
184 static JSClass JSRubyFunctionClass = {
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
185 "RubyFunction", JSCLASS_HAS_PRIVATE,
186 /*addp*/JS_PropertyStub, /*delpr*/JS_PropertyStub,
187 /*getp*/rbsm_class_get_property, /*setp*/JS_PropertyStub,
af5695f import from tar
nazoking authored Oct 3, 2006
188 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, rbsm_class_finalize,
189 /* getObjectOps */rbsm_class_get_object_ops, /*checkAccess */NULL, /*call*/rb_smjs_value_function_callback,
190 /* construct*/NULL, /* xdrObject*/NULL, /* hasInstance*/NULL, /* mark*/NULL,
191 /* spare*/0
192 };
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
193 static JSFunctionSpec JSRubyObjectFunctions[] = {
194 {"__noSuchMethod__", rbsm_class_no_such_method, 2},
195 {"toString", rbsm_class_to_string, 0},
196 {0}
197 };
af5695f import from tar
nazoking authored Oct 3, 2006
198 static JSFunctionSpec JSRubyExceptionFunctions[] = {
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
199 {"toString", rbsm_rubyexception_to_string, 0},
af5695f import from tar
nazoking authored Oct 3, 2006
200 {0}
201 };
202
203 static JSClass JSRubyExceptionClass = {
204 "RubyException", JSCLASS_HAS_PRIVATE,
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
205 /*addp*/JS_PropertyStub, /*delpr*/JS_PropertyStub,
206 /*getp*/rbsm_error_get_property, /*setp*/JS_PropertyStub,
af5695f import from tar
nazoking authored Oct 3, 2006
207 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, rbsm_rberror_finalize,
208 JSCLASS_NO_OPTIONAL_MEMBERS
209 };
210
211 static JSObjectOps rbsm_FunctionOps;
212
213 // convert -------------------------------------------------------
214 /*
215 =begin
216 JS変数をRubyのStringオブジェクトに変更
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
217 Convert a JavaScript variable to a Ruby String
af5695f import from tar
nazoking authored Oct 3, 2006
218 =end
219 */
220 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
221 rb_smjs_to_s( JSContext* cx, jsval value ){
af5695f import from tar
nazoking authored Oct 3, 2006
222 JSString* str = JS_ValueToString( cx, value );
223 return rb_str_new( JS_GetStringBytes( str ), JS_GetStringLength( str ) );
224 }
225
226 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
227 rb_smjs_to_bool( JSContext* cx, jsval value ){
af5695f import from tar
nazoking authored Oct 3, 2006
228 JSBool bp;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
229 if( !JS_ValueToBoolean( cx, value, &bp ) ){
230 rb_raise( eJSConvertError, "can't convert to boolean" );
af5695f import from tar
nazoking authored Oct 3, 2006
231 }
232 return bp ? Qtrue : Qfalse;
233 }
234
235 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
236 rb_smjs_to_a( JSContext* cx, jsval value, int shallow ){
af5695f import from tar
nazoking authored Oct 3, 2006
237 VALUE ary;
238 VALUE r;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
239 if( JSVAL_IS_OBJECT( value ) ){
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
240 JSObject* jo = JSVAL_TO_OBJECT( value );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
241 if( JSVAL_IS_NULL( value ) ) return rb_ary_new( );
242 if( JS_IsArrayObject( cx, jo ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
243 jsuint length;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
244 if( JS_HasArrayLength( cx, jo, &length ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
245 jsuint i;
246 ary = rb_ary_new2( length );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
247 for( i = 0 ; i < length ; i++ ){
af5695f import from tar
nazoking authored Oct 3, 2006
248 jsval v;
249 if( JS_GetElement( cx, jo, i, &v ) ){
250 VALUE va;
251 if( shallow ){
252 va = rb_smjs_convert_prim( cx, v );
253 }else{
254 va = rb_smjs_convertvalue( cx, v );
255 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
256 rb_ary_store( ary, i, va );
af5695f import from tar
nazoking authored Oct 3, 2006
257 }else{
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
258 rb_raise( eJSConvertError, "Fail convert to array[]" );
af5695f import from tar
nazoking authored Oct 3, 2006
259 }
260 }
261 return ary;
262 }
263 }
264 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
265 r = rb_smjs_convertvalue( cx, value );
266 return rb_funcall( r, rb_intern( "to_a" ), 0 );
af5695f import from tar
nazoking authored Oct 3, 2006
267 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
268 typedef struct{
af5695f import from tar
nazoking authored Oct 3, 2006
269 VALUE value;
270 int shallow;
271 }sRBSM_toHash;
272
273 static void
274 rb_smjs_to_h_yield( sSMJS_Enumdata* enm ){
275 sRBSM_toHash* dat;
276 VALUE val;
277 dat = (sRBSM_toHash*)(enm->data);
278 if( dat->shallow ){
279 val = rb_smjs_convertvalue( enm->cx, enm->val );
280 }else{
281 val = rb_smjs_convert_prim( enm->cx, enm->val );
282 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
283 rb_hash_aset( dat->value, rb_smjs_convertvalue( enm->cx, enm->key ), val );
af5695f import from tar
nazoking authored Oct 3, 2006
284 }
285
286 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
287 rb_smjs_to_h( JSContext* cx, jsval value, int shallow ){
af5695f import from tar
nazoking authored Oct 3, 2006
288 sRBSM_toHash edat;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
289 VALUE ret = rb_hash_new( );
290 if( !JSVAL_IS_OBJECT( value ) ){
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
291 rb_raise( eJSConvertError, "Value is not an object" );
af5695f import from tar
nazoking authored Oct 3, 2006
292 }else{
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
293 if( JSVAL_IS_NULL( value ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
294 return ret;
295 }
296 }
297 edat.shallow = shallow;
298 edat.value = ret;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
299 rbsm_each( cx, value, rb_smjs_to_h_yield, (void*)&edat );
af5695f import from tar
nazoking authored Oct 3, 2006
300 return edat.value;
301 }
302
303 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
304 rb_smjs_to_i( JSContext* cx, jsval value ){
af5695f import from tar
nazoking authored Oct 3, 2006
305 jsint ip;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
306 if( JS_ValueToInt32( cx, value, &ip ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
307 return INT2NUM( ip );
308 }else{
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
309 rb_raise( eJSConvertError, "Failed to convert value to integer" );
af5695f import from tar
nazoking authored Oct 3, 2006
310 }
311 }
312
313 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
314 rb_smjs_to_f( JSContext* cx, jsval value ){
af5695f import from tar
nazoking authored Oct 3, 2006
315 jsdouble d;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
316 if( JS_ValueToNumber( cx, value, &d ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
317 return rb_float_new( d );
318 }else{
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
319 rb_raise( eJSConvertError, "Failed to convert value to float" );
af5695f import from tar
nazoking authored Oct 3, 2006
320 }
321 }
322
323 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
324 rb_smjs_to_num( JSContext* cx, jsval value ){
325 if( JSVAL_IS_INT( value ) ){
326 return rb_smjs_to_i( cx, value );
327 }else if( JSVAL_IS_DOUBLE( value ) ){
328 return rb_smjs_to_f( cx, value );
af5695f import from tar
nazoking authored Oct 3, 2006
329 }else{
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
330 rb_raise( eJSConvertError, "Value is not a number" );
af5695f import from tar
nazoking authored Oct 3, 2006
331 }
332 }
333
334 static VALUE
335 rb_smjs_convertvalue( JSContext* cx, jsval value ){
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
336 JSObject* jo;
337 sSMJS_Class* so;
af5695f import from tar
nazoking authored Oct 3, 2006
338 JSType t = JS_TypeOfValue( cx, value );
339 switch( t ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
340 case JSTYPE_VOID: return Qnil;
341 case JSTYPE_STRING: return rb_smjs_to_s( cx, value );
342 case JSTYPE_BOOLEAN: return rb_smjs_to_bool( cx, value );
343 case JSTYPE_OBJECT:
344 if( JSVAL_IS_NULL( value ) ) return Qnil;
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
345 jo = JSVAL_TO_OBJECT( value );
346 so = JS_GetInstancePrivate( cx, jo, &JSRubyObjectClass, NULL );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
347 if( so ){
348 return so->rbobj;
349 }
350 if( JS_IsArrayObject( cx, JSVAL_TO_OBJECT( value ) ) )
351 return rb_smjs_to_a( cx, value, RBSM_CONVERT_DEEP );
352 return rb_smjs_to_h( cx, value, RBSM_CONVERT_DEEP );
353 case JSTYPE_NUMBER:
354 return rb_smjs_to_num( cx, value );
355 case JSTYPE_FUNCTION:
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
356 //rb_raise ( eJSConvertError, "function no support [%s]", JS_GetStringBytes(JS_ValueToString(cx, value)) ); break;
357 rb_raise ( eJSConvertError, "Unsupported: cannot convert JavaScript function to Ruby" ); break;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
358 default:
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
359 rb_raise ( eJSConvertError, "Unsupported object type" );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
360 break;
af5695f import from tar
nazoking authored Oct 3, 2006
361 }
362 }
363
364 // Rubyの文字列を jsvalへ変換
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
365 // Convert a Ruby string to a JavaScript value.
af5695f import from tar
nazoking authored Oct 3, 2006
366 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
367 rbsm_rubystring_to_jsval( JSContext* cx, VALUE rval, jsval* jval ){
af5695f import from tar
nazoking authored Oct 3, 2006
368 JSString* jsstr;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
369 if( (jsstr = JS_NewStringCopyZ( cx, StringValuePtr( rval ) ) ) ){
370 *jval = STRING_TO_JSVAL( jsstr );
af5695f import from tar
nazoking authored Oct 3, 2006
371 return JS_TRUE;
372 }
373 return JS_FALSE;
374 }
375
376 // Rubyの変数VALUE rval を jsval jval へ変換
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
377 // Convert a Ruby value to a JavaScript value.
af5695f import from tar
nazoking authored Oct 3, 2006
378 static JSBool
379 rb_smjs_ruby_to_js( JSContext* cx, VALUE rval, jsval* jval ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
380 if( rb_obj_is_instance_of( rval, cJSValue ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
381 sSMJS_Value* sv;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
382 Data_Get_Struct( rval, sSMJS_Value, sv );
af5695f import from tar
nazoking authored Oct 3, 2006
383 *jval = sv->value;
384 return JS_TRUE;
385 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
386 switch( TYPE( rval ) ){
387 case T_STRING: return rbsm_rubystring_to_jsval( cx, rval, jval );
0c17a62 @matthewd Convert appropriately-ranged Fixnum values to JS integers, falling back
matthewd authored Jan 20, 2008
388 case T_FIXNUM:
389 if( NUM2LONG( rval ) >= INT_MIN && NUM2LONG( rval ) <= INT_MAX && INT_FITS_IN_JSVAL( NUM2INT( rval ) ) ){
390 *jval = INT_TO_JSVAL( NUM2INT( rval ) );
391 break;
392 }
393 case T_FLOAT:
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
394 case T_BIGNUM: return JS_NewDoubleValue( cx, NUM2DBL( rval ), jval );
395 case T_TRUE: *jval = JSVAL_TRUE; break;
396 case T_FALSE: *jval = JSVAL_FALSE; break;
397 case T_NIL: *jval = JSVAL_NULL; break;
398 default:
399 *jval = OBJECT_TO_JSVAL( rbsm_ruby_to_jsobject( cx, rval ) );
af5695f import from tar
nazoking authored Oct 3, 2006
400 }
401 return JS_TRUE;
402 }
403
404 // 適度コンバートする 適度=プリミティブ値、nullのみ
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
405 // Convert a JavaScript value to a Ruby value.
af5695f import from tar
nazoking authored Oct 3, 2006
406 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
407 rb_smjs_convert_prim( JSContext* cx, jsval value ){
408 JSType t = JS_TypeOfValue( cx, value );
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
409 JSObject* jo;
410 sSMJS_Class* so;
af5695f import from tar
nazoking authored Oct 3, 2006
411 VALUE context;
412 switch( t ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
413 case JSTYPE_VOID: return Qnil;
414 case JSTYPE_STRING: return rb_smjs_to_s( cx, value );
415 case JSTYPE_BOOLEAN: return rb_smjs_to_bool( cx, value );
416 case JSTYPE_NUMBER: return rb_smjs_to_num( cx, value );
417 case JSTYPE_OBJECT:
418 if( JSVAL_IS_NULL( value ) ) return Qnil;
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
419 jo = JSVAL_TO_OBJECT( value );
420 so = JS_GetInstancePrivate( cx, jo, &JSRubyObjectClass, NULL );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
421 if( so ){
422 return so->rbobj;
423 }
5fa89b7 @matthewd Make convert_prim handle arrays.
matthewd authored Feb 21, 2008
424 if( JS_IsArrayObject( cx, jo ) )
425 return rb_smjs_to_a( cx, value, RBSM_CONVERT_SHALLOW );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
426 default:
427 context = RBSMContext_FROM_JsContext( cx );
428 return rb_smjs_value_new_jsval( context, value );
af5695f import from tar
nazoking authored Oct 3, 2006
429 }
430 }
431
432 // etc ------------------------------------------------------------------
433
434 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
435 RBSMContext_FROM_JsContext( JSContext* cx ){
436 return (VALUE)JS_GetContextPrivate( cx );
af5695f import from tar
nazoking authored Oct 3, 2006
437 }
438
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
439 static VALUE
440 rb_smjs_context_get_global( VALUE self ){
441 return rb_iv_get( self, RBSMJS_CONTEXT_GLOBAL );
af5695f import from tar
nazoking authored Oct 3, 2006
442 }
443
444 // JS例外をRuby例外として投げる
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
445 // Throw a JavaScript exception as a Ruby exception.
af5695f import from tar
nazoking authored Oct 3, 2006
446 static void
447 rb_smjs_raise_ruby( JSContext* cx ){
448 sSMJS_Error* se;
449 JSObject* jo;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
450 jsval jsvalerror = 0;
451 VALUE context = RBSMContext_FROM_JsContext( cx );
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
452 VALUE self;
af5695f import from tar
nazoking authored Oct 3, 2006
453
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
454 if( !(JS_IsExceptionPending( cx ) && JS_GetPendingException( cx, &jsvalerror ) ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
455 sSMJS_Context* cs;
02b416c @matthewd More attempts to get exception handling to behave as I want it to, and
matthewd authored Jul 28, 2007
456 char tmpmsg[BUFSIZ];
457
af5695f import from tar
nazoking authored Oct 3, 2006
458 Data_Get_Struct( context, sSMJS_Context, cs );
459 jsvalerror = cs->last_exception;
02b416c @matthewd More attempts to get exception handling to behave as I want it to, and
matthewd authored Jul 28, 2007
460
af5695f import from tar
nazoking authored Oct 3, 2006
461 if( !jsvalerror ){
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
462 rb_raise( eJSError, "No exception for error: %s", cs->last_message );
af5695f import from tar
nazoking authored Oct 3, 2006
463 }
02b416c @matthewd More attempts to get exception handling to behave as I want it to, and
matthewd authored Jul 28, 2007
464
465 cs->last_exception = 0;
466 strncpy( tmpmsg, cs->last_message, BUFSIZ );
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
467 sprintf( cs->last_message, "Exception already passed to Ruby: %s", tmpmsg );
af5695f import from tar
nazoking authored Oct 3, 2006
468 }
469
02b416c @matthewd More attempts to get exception handling to behave as I want it to, and
matthewd authored Jul 28, 2007
470 JS_ClearPendingException( cx );
471
bd5640f @matthewd Don't force all JS-originated exceptions to be JSObjects... we only care
matthewd authored Jan 20, 2008
472 if( JSVAL_IS_OBJECT( jsvalerror ) &&
473 ( jo = JSVAL_TO_OBJECT( jsvalerror ) ) ) {
474 // 元がRuby例外ならそれを継続
475 // If it was originally a Ruby exception, we continue that.
476 se = JS_GetInstancePrivate( cx, jo, &JSRubyExceptionClass, NULL );
477 if( se ){
478 int st = se->status;
479 se->status = 0;
480 rb_jump_tag( st );
481 }
af5695f import from tar
nazoking authored Oct 3, 2006
482 }
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
483
af5695f import from tar
nazoking authored Oct 3, 2006
484 // 元がJS例外なら EvalError を作成してRubyに投げる
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
485 // If the exception originated with JavaScript, we build and throw an
486 // EvalError to Ruby.
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
487 self = rbsm_evalerror_new_jsval( context, jsvalerror );
af5695f import from tar
nazoking authored Oct 3, 2006
488
489 rb_exc_raise( self );
490 }
491
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
492 // Ruby例外をJS例外として投げる
493 // Throw a Ruby exception as a JavaScript exception.
494 static JSBool
495 rb_smjs_raise_js( JSContext* cx, int status ){
496 JSObject* jo;
3d1a24b @matthewd Do some gymnastics to help a Ruby process reconstruct a useful
matthewd authored Jan 20, 2008
497 sSMJS_Error* se;
498 sSMJS_Value* sv;
499 jsval stack_string;
500 VALUE rb_g;
501 VALUE rb_e = rb_gv_get( "$!" );
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
502
503 VALUE context = RBSMContext_FROM_JsContext( cx );
504 sSMJS_Context* cs;
505 Data_Get_Struct( context, sSMJS_Context, cs );
506
3d1a24b @matthewd Do some gymnastics to help a Ruby process reconstruct a useful
matthewd authored Jan 20, 2008
507 trace("rb_smjs_raise_js(cx=%x, status=%x)", cx, status);
508
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
509 jo = JS_NewObject( cx, &JSRubyExceptionClass, NULL, NULL );
510 JS_DefineFunctions( cx, jo, JSRubyExceptionFunctions );
3d1a24b @matthewd Do some gymnastics to help a Ruby process reconstruct a useful
matthewd authored Jan 20, 2008
511
512 rb_g = rb_iv_get( context, RBSMJS_CONTEXT_GLOBAL );
513 Data_Get_Struct( rb_g, sSMJS_Value, sv );
514
515 if( JS_CallFunctionName( cx, JSVAL_TO_OBJECT( sv->value ), "__getStack__", 0, NULL, &stack_string ) ){
516 VALUE rb_stack = rb_smjs_to_s( cx, stack_string );
517
518 VALUE stack_list = rb_iv_get( rb_e, "@all_stacks" );
519 if( !RTEST( stack_list ) ){
520 stack_list = rb_ary_new();
521 rb_iv_set( rb_e, "@all_stacks", stack_list );
522 }
523
524 JS_SetProperty( cx, jo, "stack", &stack_string );
525
526 {
527 VALUE new_entry = rb_ary_new();
528 rb_ary_push( new_entry, ID2SYM( rb_intern( "js" ) ) );
529 rb_ary_push( new_entry, rb_stack );
530
531 rb_ary_push( stack_list, new_entry );
532 }
533
534 {
535 VALUE new_entry = rb_ary_new();
536 rb_ary_push( new_entry, ID2SYM( rb_intern( "ruby" ) ) );
537 rb_ary_push( new_entry, rb_funcall( Qnil, rb_intern( "caller" ), 0) );
538
539 rb_ary_push( stack_list, new_entry );
540 }
541 } else {
542 trace("Failure calling __getStack__!");
543 }
544
545 se = JS_malloc( cx, sizeof( sSMJS_Error ) );
546 se->status = status;
547 se->errinfo = rb_obj_dup( rb_e );
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
548 JS_SetPrivate( cx, jo, (void*)se );
3d1a24b @matthewd Do some gymnastics to help a Ruby process reconstruct a useful
matthewd authored Jan 20, 2008
549
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
550 cs->last_exception = OBJECT_TO_JSVAL( jo );
3d1a24b @matthewd Do some gymnastics to help a Ruby process reconstruct a useful
matthewd authored Jan 20, 2008
551 JS_SetPendingException( cx, cs->last_exception );
101e551 @matthewd Shuffle a couple of functions.
matthewd authored Jan 20, 2008
552
553 return JS_FALSE;
554 }
555
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
556 static const JSErrorFormatString*
557 rbsm_GetErrorMessage( void* userRef, const char* locale, const uintN errorNumber ){
af5695f import from tar
nazoking authored Oct 3, 2006
558 return &rbsm_ErrorFormatString[errorNumber];
559 }
560
561 // JS例外として投げられたRuby例外の文字化
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
562 // String conversion of a Ruby exception that has been thrown as a
563 // JavaScript exception.
af5695f import from tar
nazoking authored Oct 3, 2006
564 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
565 rbsm_rubyexception_to_string( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval ){
af5695f import from tar
nazoking authored Oct 3, 2006
566 sSMJS_Error* se;
567 VALUE msg;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
568 se = JS_GetInstancePrivate( cx, obj, &JSRubyExceptionClass, NULL );
569 if( !se ){
af5695f import from tar
nazoking authored Oct 3, 2006
570 // TODO: 正しい関数名、オブジェクト名を出す
b56b341 @matthewd Corrected typo in constant name.
matthewd authored Jan 20, 2008
571 // TODO: Output the correct object and function names
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
572 JS_ReportErrorNumber( cx, rbsm_GetErrorMessage, NULL, RBSMMSG_INCOMPATIBLE_PROTO, "RubyException", "toString", "Object" );
af5695f import from tar
nazoking authored Oct 3, 2006
573 return JS_FALSE;
574 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
575 msg = rb_funcall( se->errinfo, rb_intern( "to_s" ), 0 );
576 rbsm_rubystring_to_jsval( cx, msg, rval );
af5695f import from tar
nazoking authored Oct 3, 2006
577 return JS_TRUE;
578 }
579
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
580 // jsval -> RubyObject のハッシュテーブル : Hash Table
af5695f import from tar
nazoking authored Oct 3, 2006
581 static JSHashNumber
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
582 jsid2hash( const void* key ){
af5695f import from tar
nazoking authored Oct 3, 2006
583 return (JSHashNumber)key;
584 }
585 static intN
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
586 jsidCompare( const void* v1, const void* v2 ){
af5695f import from tar
nazoking authored Oct 3, 2006
587 return (jsid)v1 == (jsid)v2;
588 }
589
590 static intN
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
591 rbobjCompare( const void* v1, const void* v2 ){
af5695f import from tar
nazoking authored Oct 3, 2006
592 return (VALUE)v1 == (VALUE)v2;
593 }
594
595 static void
596 rbsm_set_jsval_to_rbval( sSMJS_Context* cs, VALUE self, jsval value ){
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
597 char pname[10];
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
598 JSHashEntry* x = JS_HashTableAdd( cs->id2rbval, (const void*)value, (void*)rb_obj_id( self ) );
599 if( !x ) rb_raise( eJSError, "fail to set rbval to HashTable" );
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
600 // JS-GC対策用storeに値を設定 : Setting value to the one for JS-GC measure store
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
601 sprintf( pname, "%x", (int)value );
af5695f import from tar
nazoking authored Oct 3, 2006
602 JS_SetProperty( cs->cx, cs->store, pname, &value );
603 }
604
605 static VALUE
606 rbsm_get_jsval_to_rbval( sSMJS_Context* cs, jsval value ){
607 VALUE rid = (VALUE)JS_HashTableLookup( cs->id2rbval, (const void*)value );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
608 return rb_funcall( rb_const_get( rb_cObject, rb_intern( "ObjectSpace" ) ), rb_intern( "_id2ref" ), 1, rid );
af5695f import from tar
nazoking authored Oct 3, 2006
609 }
610
611 static JSBool
612 rbsm_lookup_jsval_to_rbval( sSMJS_Context* cs, jsid value ){
613 return NULL != JS_HashTableLookup( cs->id2rbval, (const void*)value );
614 }
615
616 static JSBool
617 rbsm_remove_jsval_to_rbval( sSMJS_Context* cs, jsval value ){
618 char pname[10];
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
619
620 sprintf( pname, "%x", (int)value );
af5695f import from tar
nazoking authored Oct 3, 2006
621 JS_DeleteProperty( cs->cx, cs->store, pname );
622 return JS_HashTableRemove( cs->id2rbval, (const void*)value );
623 }
624
625 static JSContext*
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
626 RBSMContext_TO_JsContext( VALUE context ){
af5695f import from tar
nazoking authored Oct 3, 2006
627 sSMJS_Context* cs;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
628
af5695f import from tar
nazoking authored Oct 3, 2006
629 Data_Get_Struct( context, sSMJS_Context, cs );
630 return cs->cx;
631 }
632
633 static jsval
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
634 rb_smjs_evalscript( sSMJS_Context* cs, JSObject* obj, int argc, VALUE* argv ){
af5695f import from tar
nazoking authored Oct 3, 2006
635 char* source;
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
636 char* filename;
637 int lineno;
af5695f import from tar
nazoking authored Oct 3, 2006
638 jsval value;
639 JSBool ok;
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
640 VALUE code;
641 VALUE file;
642 VALUE line;
643
644 rb_scan_args( argc, argv, "12", &code, &file, &line );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
645
646 SafeStringValue( code );
647 source = StringValuePtr( code );
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
648
649 if (NIL_P(file)) {
650 filename = NULL;
651 } else {
652 SafeStringValue( file );
653 filename = StringValuePtr( file );
654 }
655
656 if (NIL_P(line))
657 lineno = 1;
658 else
659 lineno = NUM2INT( line );
660
02b416c @matthewd More attempts to get exception handling to behave as I want it to, and
matthewd authored Jul 28, 2007
661 //cs->last_exception = 0;
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
662 ok = JS_EvaluateScript( cs->cx, obj, source, strlen( source ), filename, lineno, &value );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
663 if( !ok ) rb_smjs_raise_ruby( cs->cx );
af5695f import from tar
nazoking authored Oct 3, 2006
664 return value;
665 }
666
667 static jsval
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
668 rb_smjs_value_evalscript( int argc, VALUE* argv, VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
669 sSMJS_Value* sv;
670 Data_Get_Struct( self, sSMJS_Value, sv );
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
671 return rb_smjs_evalscript( sv->cs, JSVAL_TO_OBJECT( sv->value ), argc, argv );
af5695f import from tar
nazoking authored Oct 3, 2006
672 }
673
674 static void
675 rbsm_each_raise( sSMJS_Enumdata* enm, const char* msg ){
676 JS_DestroyIdArray( enm->cx, enm->ida );
b56b341 @matthewd Corrected typo in constant name.
matthewd authored Jan 20, 2008
677 rb_raise( eJSConvertError, "%s", msg );
af5695f import from tar
nazoking authored Oct 3, 2006
678 }
679
680 static void*
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
681 rbsm_each( JSContext* cx, jsval value, RBSMJS_YIELD yield, void* data ){
af5695f import from tar
nazoking authored Oct 3, 2006
682 sSMJS_Enumdata enmdata;
683 sSMJS_Enumdata* enm;
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
684 JSObject* jo;
af5695f import from tar
nazoking authored Oct 3, 2006
685
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
686 enm = &enmdata;
af5695f import from tar
nazoking authored Oct 3, 2006
687 enm->data = data;
688 enm->cx = cx;
689 enm->ida = NULL;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
690 enm->obj = JSVAL_TO_OBJECT( value );
af5695f import from tar
nazoking authored Oct 3, 2006
691 if( JSVAL_IS_PRIMITIVE( value ) ){
692 if( JSVAL_IS_STRING( value ) ){
693 return enm->data;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
694 //rb_raise ( eJSConvertError, "can't enumerate" );
af5695f import from tar
nazoking authored Oct 3, 2006
695 }else{
696 return enm->data;
697 }
698 }else{
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
699 enm->ida = JS_Enumerate( enm->cx, enm->obj );
af5695f import from tar
nazoking authored Oct 3, 2006
700 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
701 if( !enm->ida ){
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
702 rb_raise( eJSConvertError, "Unable to enumerate" );
af5695f import from tar
nazoking authored Oct 3, 2006
703 }
704
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
705 for( enm->i = 0; enm->i < enm->ida->length; enm->i++ ){
af5695f import from tar
nazoking authored Oct 3, 2006
706 enm->id = enm->ida->vector[enm->i];
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
707 if( JS_IdToValue( enm->cx, enm->id, &enm->key ) ){
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
708 //enm->keystr = JS_GetStringBytes( JS_ValueToString( cx, enm->key ) );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
709 //if( JS_GetProperty( enm->cx, enm->obj, enm->keystr, &enm->val ) ){
710 if( OBJ_GET_PROPERTY( enm->cx, enm->obj, enm->id, &enm->val ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
711 yield( enm );
712 }else{
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
713 rbsm_each_raise( enm, "Unable to get property" );
af5695f import from tar
nazoking authored Oct 3, 2006
714 }
715 }else{
3662e6f @matthewd Tidied up a bunch of error messages.
matthewd authored Jan 20, 2008
716 rbsm_each_raise( enm, "Unable to get key name" );
af5695f import from tar
nazoking authored Oct 3, 2006
717 }
718 }
719
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
720 JS_DestroyIdArray( enm->cx, enm->ida );
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
721 jo = JS_GetPrototype( enm->cx, enm->obj );
af5695f import from tar
nazoking authored Oct 3, 2006
722 if( jo ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
723 return rbsm_each( enm->cx, OBJECT_TO_JSVAL( jo ), yield, enm->data );
af5695f import from tar
nazoking authored Oct 3, 2006
724 }
725 return enm->data;
726 }
727
728 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
729 rbsm_value_each( VALUE self, RBSMJS_YIELD yield, void* data ){
af5695f import from tar
nazoking authored Oct 3, 2006
730 sSMJS_Value* sv;
731 Data_Get_Struct( self, sSMJS_Value, sv );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
732 return (VALUE)rbsm_each( sv->cs->cx, sv->value, yield, data );
af5695f import from tar
nazoking authored Oct 3, 2006
733 }
734
735 static void
736 rb_smjs_value_each_yield( sSMJS_Enumdata* enm ){
737 rb_yield( rb_smjs_convert_prim( enm->cx, enm->val ) );
738 }
739
740 static void
741 rb_smjs_value_each_with_index_yield( sSMJS_Enumdata* enm ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
742 rb_yield_values( 2, rb_smjs_convertvalue( enm->cx, enm->key ), rb_smjs_convert_prim( enm->cx, enm->val ) );
af5695f import from tar
nazoking authored Oct 3, 2006
743 }
744
745 // protect 内で proc を呼ぶ。引数 args は rb_ary で、最後に proc が入っている
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
746 // Calls a Proc from inside protect; args is a Ruby array with the Proc
747 // appended.
af5695f import from tar
nazoking authored Oct 3, 2006
748 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
749 rb_smjs_ruby_proc_caller( VALUE args ){
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
750 // Procを実行 : The Proc to execute
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
751 VALUE proc;
752 proc = rb_ary_pop( args );
753 return rb_apply( proc, rb_intern( "call" ), args );
af5695f import from tar
nazoking authored Oct 3, 2006
754 }
755
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
756 static VALUE
96aef63 @matthewd Split out Context#flush, which resets the contents of the JS global
matthewd authored Jan 20, 2008
757 rb_smjs_ruby_proc_caller3( VALUE proc ){
758 return rb_funcall( proc, rb_intern( "call" ), 0 );
759 }
760
761 static VALUE
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
762 rb_smjs_ruby_string_caller( VALUE obj ){
763 return rb_funcall( obj, rb_intern( "to_s" ), 0 );
764 }
765
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
766 // used by Object#[] below
767 static VALUE
768 rb_smjs_ruby_box_caller( VALUE args ){
769 VALUE obj;
770 obj = rb_ary_pop( args );
771 return rb_apply( obj, rb_intern( "[]" ), args );
772 }
773
774 static VALUE
775 rb_smjs_ruby_missing_caller( VALUE args ){
776 VALUE obj;
777 obj = rb_ary_pop( args );
778 return rb_apply( obj, rb_intern( "method_missing" ), args );
779 }
780
4081718 @matthewd Improved (fixed?) "zero-arg-call" logic.
matthewd authored Jul 28, 2007
781 #ifdef ZERO_ARITY_METHOD_IS_PROPERTY
782 struct{
783 char* keyname;
784 jsval val;
785 }g_last0arity;
786 #endif
787
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
788 // Ruby Object#[]
789 static JSBool
790 rb_smjs_value_object_callback( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval ){
791 JSObject* fobj;
792 VALUE rargs, res;
793 uintN i;
794 int status;
795 sSMJS_Class* so;
796
797 fobj = JSVAL_TO_OBJECT( argv[-2] );
4081718 @matthewd Improved (fixed?) "zero-arg-call" logic.
matthewd authored Jul 28, 2007
798
799 // If we are trying to invoke this object as a zero-param method, and
800 // it was just retrieved as a 0-arity "method as property", we
801 // silently return the object itself; the likihood that we actually
802 // meant to invoke :[] with no arguments is very low.
803 if (argc == 0 && OBJECT_TO_JSVAL(fobj) == g_last0arity.val) {
804 *rval = g_last0arity.val;
805 return JS_TRUE;
806 }
807
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
808 so = (sSMJS_Class*)JS_GetPrivate( cx, fobj );
809
810 // 引数をSpiderMonkey::Valueに : Argument in SpiderMonkey::Value
811 rargs = rb_ary_new2( argc + 1 );
812 for( i = 0 ; i < argc ; i++ )
813 rb_ary_store( rargs, i, rb_smjs_convert_prim( cx, argv[i] ) );
814 rb_ary_store( rargs, i, so->rbobj );
815
816 // proc を実行 : The Proc to execute
817 res = rb_protect( rb_smjs_ruby_box_caller, rargs, &status );
818
819 // Ruby関数実行結果、例外が投げられた場合はJS例外にラップして投げる
820 // Check the Ruby function execution result; if an exception was
821 // thrown, we raise a corresponding error in JavaScript.
822 if( status != 0 )
823 return rb_smjs_raise_js( cx, status );
824
825 return rb_smjs_ruby_to_js( cx, res, rval );
826 }
827
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
828 // Ruby proc/method がJavaScriptから呼ばれた
829 // Ruby method called from JavaScript
af5695f import from tar
nazoking authored Oct 3, 2006
830 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
831 rb_smjs_value_function_callback( JSContext* cx, JSObject* thisobj, uintN argc, jsval* argv, jsval* rval ){
af5695f import from tar
nazoking authored Oct 3, 2006
832 JSObject* fobj;
833 VALUE rargs, res;
834 uintN i;
835 int status;
836 sSMJS_Class* so;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
837
838 fobj = JSVAL_TO_OBJECT( argv[-2] );
af5695f import from tar
nazoking authored Oct 3, 2006
839 so = (sSMJS_Class*)JS_GetPrivate( cx, fobj );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
840
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
841 // 引数をSpiderMonkey::Valueに : Argument in SpiderMonkey::Value
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
842 rargs = rb_ary_new2( argc + 1 );
843 for( i = 0 ; i < argc ; i++ )
af5695f import from tar
nazoking authored Oct 3, 2006
844 rb_ary_store( rargs, i, rb_smjs_convert_prim( cx, argv[i] ) );
845 rb_ary_store( rargs, i, so->rbobj );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
846
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
847 // proc を実行 : The Proc to execute
af5695f import from tar
nazoking authored Oct 3, 2006
848 res = rb_protect( rb_smjs_ruby_proc_caller, rargs, &status );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
849
af5695f import from tar
nazoking authored Oct 3, 2006
850 // Ruby関数実行結果、例外が投げられた場合はJS例外にラップして投げる
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
851 // Check the Ruby function execution result; if an exception was
852 // thrown, we raise a corresponding error in JavaScript.
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
853 if( status != 0 )
af5695f import from tar
nazoking authored Oct 3, 2006
854 return rb_smjs_raise_js( cx, status );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
855
af5695f import from tar
nazoking authored Oct 3, 2006
856 return rb_smjs_ruby_to_js( cx, res, rval );
857 }
858
859 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
860 rb_smjs_ruby_proc_caller2( VALUE args ){
861 ID mname;
862 VALUE obj;
863
864 obj = rb_ary_pop( args );
865 mname = rb_ary_pop( args );
866 return rb_apply( obj, mname, args );
af5695f import from tar
nazoking authored Oct 3, 2006
867 }
868
869 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
870 rbsm_class_no_such_method( JSContext* cx, JSObject* thisobj, uintN argc, jsval* argv, jsval* rval ){
af5695f import from tar
nazoking authored Oct 3, 2006
871 char* keyname;
872 sSMJS_Class* so;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
873
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
874 VALUE rargs, res;
875 int status;
876
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
877 keyname = JS_GetStringBytes( JS_ValueToString( cx, argv[0] ) );
f2022bd @matthewd Improve some logging, and enable some more JSOPTIONs.
matthewd authored Jan 20, 2008
878 trace("_noSuchMethod__(cx=%x, keyname=%s)", cx, keyname );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
879 so = JS_GetInstancePrivate( cx, JSVAL_TO_OBJECT( argv[-2] ), &JSRubyObjectClass, NULL );
880 if( !so ){
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
881 so = JS_GetInstancePrivate( cx, thisobj, &JSRubyObjectClass, NULL );
882 if( !so ){
883 JS_ReportErrorNumber( cx, rbsm_GetErrorMessage, NULL, RBSMMSG_NOT_FUNCTION, keyname );
884 return JS_FALSE;
885 }
af5695f import from tar
nazoking authored Oct 3, 2006
886 }
84ef1b2 Rubyオブジェクトの引数なしメソッドをJavaScriptからプロパティーとしては呼べなくした。
nazoking authored Dec 10, 2006
887 #ifdef ZERO_ARITY_METHOD_IS_PROPERTY
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
888 if( strcmp( keyname, g_last0arity.keyname ) == 0 ){
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
889 trace(" == g_last0arity.keyname" );
af5695f import from tar
nazoking authored Oct 3, 2006
890 *rval = g_last0arity.val;
891 return JS_TRUE;
892 }
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
893 trace(" != g_last0arity.keyname [%s]", g_last0arity.keyname );
84ef1b2 Rubyオブジェクトの引数なしメソッドをJavaScriptからプロパティーとしては呼べなくした。
nazoking authored Dec 10, 2006
894 #endif
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
895
896 if (argc != 2)
897 return JS_FALSE;
898
899 // 引数をSpiderMonkey::Valueに : Argument in SpiderMonkey::Value
900 rargs = rb_smjs_convertvalue( cx, argv[1] );
901 rb_ary_unshift( rargs, ID2SYM( rb_intern( keyname ) ) );
902 rb_ary_push( rargs, so->rbobj );
903
904 // proc を実行 : The Proc to execute
905 res = rb_protect( rb_smjs_ruby_missing_caller, rargs, &status );
906
907 // Ruby関数実行結果、例外が投げられた場合はJS例外にラップして投げる
908 // Check the Ruby function execution result; if an exception was
909 // thrown, we raise a corresponding error in JavaScript.
910 if( status != 0 )
911 return rb_smjs_raise_js( cx, status );
912
913 return rb_smjs_ruby_to_js( cx, res, rval );
af5695f import from tar
nazoking authored Oct 3, 2006
914 }
915
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
916 static JSBool
917 rbsm_class_to_string( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval ){
918 sSMJS_Class* so;
919 VALUE res;
920 int status;
921
922 so = JS_GetInstancePrivate( cx, JSVAL_TO_OBJECT( argv[-2] ), &JSRubyObjectClass, NULL );
923 if( !so ){
924 so = JS_GetInstancePrivate( cx, obj, &JSRubyObjectClass, NULL );
925 if( !so ){
926 JS_ReportErrorNumber( cx, rbsm_GetErrorMessage, NULL, RBSMMSG_INCOMPATIBLE_PROTO, "RubyObject", "toString", "Object" );
927 return JS_FALSE;
928 }
929 }
930
931 res = rb_protect( rb_smjs_ruby_string_caller, so->rbobj, &status );
932
933 // Check the Ruby function execution result; if an exception was
934 // thrown, we raise a corresponding error in JavaScript.
935 if( status != 0 )
936 return rb_smjs_raise_js( cx, status );
937
938 return rb_smjs_ruby_to_js( cx, res, rval );
939 }
940
941
af5695f import from tar
nazoking authored Oct 3, 2006
942 static JSObjectOps*
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
943 rbsm_class_get_object_ops( JSContext* cx, JSClass* clasp ){
af5695f import from tar
nazoking authored Oct 3, 2006
944 return &rbsm_FunctionOps;
945 }
946
947 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
948 rbsm_error_get_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp ){
949 sSMJS_Error* se;
950 se = JS_GetInstancePrivate( cx, obj, &JSRubyExceptionClass, NULL );
af5695f import from tar
nazoking authored Oct 3, 2006
951 if( !se ){
952 // TODO: 正しい関数名、オブジェクト名を出す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
953 // TODO: Output the correct object and function names
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
954 char* keyname = JS_GetStringBytes( JS_ValueToString( cx, id ) );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
955 JS_ReportErrorNumber( cx, rbsm_GetErrorMessage, NULL, RBSMMSG_INCOMPATIBLE_PROTO, "RubyObject", keyname, "Object" );
af5695f import from tar
nazoking authored Oct 3, 2006
956 return JS_FALSE;
957 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
958 return rbsm_get_ruby_property( cx, obj, id, vp, se->errinfo );
af5695f import from tar
nazoking authored Oct 3, 2006
959 }
960
961 static void
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
962 rbsm_rberror_finalize( JSContext* cx, JSObject* obj ){
963 sSMJS_Error* se;
964 se = (sSMJS_Error*)JS_GetPrivate( cx, obj );
965 if( se ){
966 JS_free( cx, se );
af5695f import from tar
nazoking authored Oct 3, 2006
967 }
968 }
969
970 static sSMJS_Class*
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
971 rbsm_wrap_class( JSContext* cx, VALUE rbobj ){
af5695f import from tar
nazoking authored Oct 3, 2006
972 sSMJS_Class* so;
973 VALUE context;
974
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
975 so = (sSMJS_Class*)malloc( sizeof( sSMJS_Class ) );
af5695f import from tar
nazoking authored Oct 3, 2006
976 so->rbobj = rbobj;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
977 context = RBSMContext_FROM_JsContext( cx );
af5695f import from tar
nazoking authored Oct 3, 2006
978 rb_hash_aset( rb_iv_get( context, RBSMJS_CONTEXT_BINDINGS ), rb_obj_id( rbobj ), rbobj );
979 return so;
980 }
981
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
982 // Ruby のProcを JSのfunctionとしてラップして返す
983 // Convert a Ruby Proc to a JavaScript function
af5695f import from tar
nazoking authored Oct 3, 2006
984 static JSObject*
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
985 rbsm_proc_to_function( JSContext* cx, VALUE proc ){
af5695f import from tar
nazoking authored Oct 3, 2006
986 JSObject* jo;
987 sSMJS_Class* so;
988
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
989 so = rbsm_wrap_class( cx, proc );
990 jo = JS_NewObject( cx, &JSRubyFunctionClass, NULL, NULL );
f2022bd @matthewd Improve some logging, and enable some more JSOPTIONs.
matthewd authored Jan 20, 2008
991 trace("rbsm_proc_to_function(cx=%x, obj=%x); [count %d -> %d]", cx, jo, alloc_count_rb2js, ++alloc_count_rb2js);
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
992 so->jsv = OBJECT_TO_JSVAL( jo );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
993 JS_SetPrivate( cx, jo, (void*)so );
af5695f import from tar
nazoking authored Oct 3, 2006
994 return jo;
995 }
996
997 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
998 rbsm_get_ruby_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp, VALUE rbobj ){
999 char* keyname;
af5695f import from tar
nazoking authored Oct 3, 2006
1000 ID rid;
1001 VALUE method;
1002 int iarity;
1003 VALUE ret;
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
1004
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
1005 keyname = JS_GetStringBytes( JS_ValueToString( cx, id ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1006
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
1007 // FIXME: Every rb_funcall() below should be protected.
1008
f2022bd @matthewd Improve some logging, and enable some more JSOPTIONs.
matthewd authored Jan 20, 2008
1009 trace( "_get_property_(cx=%x, keyname=%s)", cx, keyname );
af5695f import from tar
nazoking authored Oct 3, 2006
1010 rid = rb_intern( keyname );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1011 // TODO: int rb_respond_to( VALUE obj, ID id )
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
1012 if( rb_is_const_id( rid ) &&
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
1013 RTEST( rb_funcall( rbobj, rb_intern( "respond_to?" ), 1, ID2SYM( rb_intern( "constants" ) ) ) ) &&
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
1014 rb_const_defined( rbobj, rid ) ){
1015 // 定数はその値を返す
1016 // Constant returns the value
1017 return rb_smjs_ruby_to_js( cx, rb_const_get( rbobj, rid ), vp );
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
1018 }else if( RTEST( rb_funcall( rbobj, rb_intern( "respond_to?" ), 1, ID2SYM( rid ) ) ) ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1019 method = rb_funcall( rbobj, rb_intern( "method" ), 1, ID2SYM( rid ) );
84ef1b2 Rubyオブジェクトの引数なしメソッドをJavaScriptからプロパティーとしては呼べなくした。
nazoking authored Dec 10, 2006
1020 #ifdef ZERO_ARITY_METHOD_IS_PROPERTY
af5695f import from tar
nazoking authored Oct 3, 2006
1021 // メソッドの引数の数を調べる
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1022 // We check the number of arguments the method takes
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1023 iarity = NUM2INT( rb_funcall( method, rb_intern( "arity" ), 0 ) );
1024 if( iarity == 0 /*|| iarity == -1*/ ){
4081718 @matthewd Improved (fixed?) "zero-arg-call" logic.
matthewd authored Jul 28, 2007
1025 JSBool success;
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
1026 int status;
af5695f import from tar
nazoking authored Oct 3, 2006
1027 // 引数0のメソッドはプロパティーとして値を取得
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1028 // A method with 0 arguments directly returns the value, acting as a property
c818e1b @matthewd Tweaked function call behaviour (RTEST on respond_to? result), and
matthewd authored Jan 20, 2008
1029 ret = rb_protect( rb_smjs_ruby_proc_caller3, method, &status );
1030 if( status != 0 )
1031 return rb_smjs_raise_js( cx, status );
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
1032 //ret = rb_funcall( rbobj, rid, 0 );
4081718 @matthewd Improved (fixed?) "zero-arg-call" logic.
matthewd authored Jul 28, 2007
1033 success = rb_smjs_ruby_to_js( cx, ret, vp );
0dfe4d7 @matthewd Some changes to JS-to-Ruby function call and object access behaviour.
matthewd authored Jul 24, 2007
1034 g_last0arity.keyname = keyname == "ID" ? "id" : keyname;
4081718 @matthewd Improved (fixed?) "zero-arg-call" logic.
matthewd authored Jul 28, 2007
1035 g_last0arity.val = *vp;
1036 return success;
af5695f import from tar
nazoking authored Oct 3, 2006
1037 }else{
1038 // 引数0以上、あるいは可変引数のメソッドはfunctionオブジェクトにして返す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1039 // Methods with more than zero arguments (or a variable number)
1040 // result in a JavaScript function object.
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1041 *vp = OBJECT_TO_JSVAL( rbsm_proc_to_function( cx, method ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1042 }
84ef1b2 Rubyオブジェクトの引数なしメソッドをJavaScriptからプロパティーとしては呼べなくした。
nazoking authored Dec 10, 2006
1043 #else
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1044 *vp = OBJECT_TO_JSVAL( rbsm_proc_to_function( cx, method ) );
84ef1b2 Rubyオブジェクトの引数なしメソッドをJavaScriptからプロパティーとしては呼べなくした。
nazoking authored Dec 10, 2006
1045 #endif
af5695f import from tar
nazoking authored Oct 3, 2006
1046 }
1047 return JS_TRUE;
1048 }
1049
1050 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1051 rbsm_set_ruby_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp, VALUE rbobj ){
1052 char* pkeyname;
af5695f import from tar
nazoking authored Oct 3, 2006
1053 char keyname[BUFSIZ];
1054 ID rid;
1055 int status;
e2a5cb4 @matthewd Made a few changes to get building to work under cl.exe.
matthewd authored Jul 24, 2007
1056 VALUE vals = rb_ary_new2( 3 );
af5695f import from tar
nazoking authored Oct 3, 2006
1057
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
1058 pkeyname = JS_GetStringBytes( JS_ValueToString( cx, id ) );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1059 sprintf( keyname, "%s=", pkeyname );
af5695f import from tar
nazoking authored Oct 3, 2006
1060 rid = rb_intern( keyname );
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1061 // 引数をSpiderMonkey::Valueに : Argument in SpiderMonkey::Value
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1062 rb_ary_push( vals, rb_smjs_convert_prim( cx, vp[0] ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1063 rb_ary_push( vals, rid );
1064 rb_ary_push( vals, rbobj );
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1065 // proc を実行 : The Proc to execute
af5695f import from tar
nazoking authored Oct 3, 2006
1066 rb_protect( rb_smjs_ruby_proc_caller2, vals, &status );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1067 if( status != 0 )
af5695f import from tar
nazoking authored Oct 3, 2006
1068 return rb_smjs_raise_js( cx, status );
1069 return JS_TRUE;
1070 }
1071
1072 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1073 rbsm_class_get_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp ){
af5695f import from tar
nazoking authored Oct 3, 2006
1074 sSMJS_Class* so;
1075
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1076 so = JS_GetInstancePrivate( cx, obj, &JSRubyObjectClass, NULL );
1077 if( !so ){
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
1078 char* keyname = JS_GetStringBytes( JS_ValueToString( cx, id ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1079 // TODO: 正しい関数名、オブジェクト名を出す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1080 // TODO: Output the correct object and function names
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1081 JS_ReportErrorNumber( cx, rbsm_GetErrorMessage, NULL, RBSMMSG_INCOMPATIBLE_PROTO, "RubyObject", keyname, "Object" );
af5695f import from tar
nazoking authored Oct 3, 2006
1082 return JS_FALSE;
1083 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1084 return rbsm_get_ruby_property( cx, obj, id, vp, so->rbobj );
af5695f import from tar
nazoking authored Oct 3, 2006
1085 }
1086
1087 static JSBool
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1088 rbsm_class_set_property( JSContext* cx, JSObject* obj, jsval id, jsval* vp ){
1089 sSMJS_Class* so = JS_GetInstancePrivate( cx, obj, &JSRubyObjectClass, NULL );
1090 if( !so ){
60dee17 @matthewd Be somewhat more paranoid about the type of the jsval we're given in a
matthewd authored Aug 8, 2007
1091 char* keyname = JS_GetStringBytes( JS_ValueToString( cx, id ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1092 // TODO: 正しい関数名、オブジェクト名を出す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1093 // TODO: Output the correct object and function names
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1094 JS_ReportErrorNumber( cx, rbsm_GetErrorMessage, NULL, RBSMMSG_INCOMPATIBLE_PROTO, "RubyObject", keyname, "Object" );
af5695f import from tar
nazoking authored Oct 3, 2006
1095 return JS_FALSE;
1096 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1097 return rbsm_set_ruby_property( cx, obj, id, vp, so->rbobj );
af5695f import from tar
nazoking authored Oct 3, 2006
1098 }
1099
455d700 @matthewd Natively convert Ruby Time and Date (and, through inheritance, DateTime)
matthewd authored Jan 20, 2008
1100
1101 static JSObject*
1102 rbsm_ruby_to_jsdate( JSContext* cx, VALUE msec ){
1103 JSObject* jsGlobal = JS_GetGlobalObject( cx );
1104 jsval msec_js, dateval;
1105
1106 rb_smjs_ruby_to_js( cx, msec, &msec_js );
1107
1108 JS_CallFunctionName( cx, jsGlobal, "__newDate__", 1, &msec_js, &dateval );
1109 return JSVAL_TO_OBJECT( dateval );
1110 }
1111
4527b8a @matthewd Define rb_ary_size(), as I seem to have imagined it.
matthewd authored Feb 27, 2008
1112 inline static long rb_ary_size(VALUE ary) { return RARRAY(ary)->len; }
1113
af5695f import from tar
nazoking authored Oct 3, 2006
1114 // rubyobj obj を javascript で使えるようにラップする
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1115 // To access a Ruby object from JavaScript, we wrap it.
af5695f import from tar
nazoking authored Oct 3, 2006
1116 static JSObject*
1117 rbsm_ruby_to_jsobject( JSContext* cx, VALUE obj ){
1118 JSObject* jo;
1119 sSMJS_Value* sv;
1120 sSMJS_Class* so;
1121
1122 if( rb_obj_is_kind_of( obj, cJSValue ) ){
1123 Data_Get_Struct( obj, sSMJS_Value, sv );
1124 return JSVAL_TO_OBJECT( sv->value );
1125 }
455d700 @matthewd Natively convert Ruby Time and Date (and, through inheritance, DateTime)
matthewd authored Jan 20, 2008
1126 if( rb_obj_is_kind_of( obj, rb_cTime ) ){
1127 trace("creating Time");
1128 VALUE msec = rb_funcall( obj, rb_intern( "to_i" ), 0 );
1129 return rbsm_ruby_to_jsdate( cx, msec );
1130 }
1131 if( rb_obj_is_kind_of( obj, rb_cDate ) ){
1132 trace("creating Date");
1133 VALUE msec = rb_funcall( rb_funcall( obj, rb_intern( "strftime" ), 1, rb_str_new2( "%s" ) ), rb_intern( "to_i" ), 0);
1134 return rbsm_ruby_to_jsdate( cx, msec );
1135 }
072eedb @matthewd Convert Ruby Array instances to JavaScript Arrays.
matthewd authored Feb 26, 2008
1136 if( rb_obj_is_kind_of( obj, rb_cArray ) ){
1137 long i;
1138 long len = rb_ary_size( obj );
1139 jsval tmp;
1140 jo = JS_NewArrayObject( cx, 0, NULL );
1141 for( i = 0; i < len; i++ ){
1142 rb_smjs_ruby_to_js( cx, rb_ary_entry( obj, i ), &tmp );
1143 JS_SetElement( cx, jo, i, &tmp );
1144 }
1145 return jo;
1146 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1147 so = rbsm_wrap_class( cx, obj );
1148 jo = JS_NewObject( cx, &JSRubyObjectClass, NULL, NULL );
f2022bd @matthewd Improve some logging, and enable some more JSOPTIONs.
matthewd authored Jan 20, 2008
1149 trace("rbsm_ruby_to_jsobject(cx=%x, obj=%x); [count %d -> %d]", cx, jo, alloc_count_rb2js, ++alloc_count_rb2js);
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1150 so->jsv = OBJECT_TO_JSVAL( jo );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1151 JS_SetPrivate( cx, jo, (void*)so );
093242d @matthewd * Add JSCLASS_GLOBAL_FLAGS to the global object, when available.
matthewd authored Jan 20, 2008
1152 JS_DefineFunctions( cx, jo, JSRubyObjectFunctions );
af5695f import from tar
nazoking authored Oct 3, 2006
1153 return jo;
1154 }
1155
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1156 // RubyObjectClass のデストラクタ : RubyObjectClass Destructor
af5695f import from tar
nazoking authored Oct 3, 2006
1157 static void
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1158 rbsm_class_finalize( JSContext* cx, JSObject* obj ){
af5695f import from tar
nazoking authored Oct 3, 2006
1159 sSMJS_Class* so;
1160
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1161 so = (sSMJS_Class*)JS_GetPrivate( cx, obj );
1162 if( so ){
f2022bd @matthewd Improve some logging, and enable some more JSOPTIONs.
matthewd authored Jan 20, 2008
1163 trace("rbsm_class_finalize(cx=%x, obj=%x); [count %d -> %d]", cx, obj, alloc_count_rb2js, --alloc_count_rb2js);
af5695f import from tar
nazoking authored Oct 3, 2006
1164 if( so->rbobj ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1165 if( JS_GetContextPrivate( cx ) ){
1166 VALUE context = RBSMContext_FROM_JsContext( cx );
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1167 VALUE bindings = rb_iv_get( context, RBSMJS_CONTEXT_BINDINGS );
1168 if( RTEST( bindings ) ){
1169 rb_hash_delete( rb_iv_get( context, RBSMJS_CONTEXT_BINDINGS ), rb_obj_id( so->rbobj ) );
1170 }
af5695f import from tar
nazoking authored Oct 3, 2006
1171 }
1172 }
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1173 if( so->jsv ){
1174 JS_RemoveRoot( cx, &(so->jsv) );
1175 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1176 free( so );
af5695f import from tar
nazoking authored Oct 3, 2006
1177 }
1178 }
1179
1180 // SpiderMonkey::Value ---------------------------------------------------------------------
1181
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1182 // SpiderMonkey::Valueのデストラクタ : SpiderMonkey::Value Destructor
af5695f import from tar
nazoking authored Oct 3, 2006
1183 static void
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1184 rb_smjs_value_free( sSMJS_Value* sv ){
1b442c7 @matthewd Add a trace() macro, to save commenting/uncommenting of the printf
matthewd authored Jan 20, 2008
1185 trace( "value_free(cx=%x, value=%x); [count %d -> %d]", sv->cs->cx, sv->value, alloc_count_js2rb, --alloc_count_js2rb );
af5695f import from tar
nazoking authored Oct 3, 2006
1186 if( sv->value && sv->cs->store ){
1187 rbsm_remove_jsval_to_rbval( sv->cs, sv->value );
1188 }
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1189 free( sv );
af5695f import from tar
nazoking authored Oct 3, 2006
1190 }
1191
1192 // Rubyレベルで作成はできないように
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1193 // SpiderMonkey::Value can't be created directly from within Ruby.
af5695f import from tar
nazoking authored Oct 3, 2006
1194 static VALUE
1195 rb_smjs_value_initialize( VALUE self, VALUE context ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1196 rb_raise( eJSError, "do not create SpiderMonkey::Value" );
af5695f import from tar
nazoking authored Oct 3, 2006
1197 return Qnil;
1198 }
1199
1200 // Valueの実際のコンストラクタ
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1201 // The actual constructor of SpiderMonkey::Value.
af5695f import from tar
nazoking authored Oct 3, 2006
1202 static VALUE
1203 rb_smjs_value_new_jsval( VALUE context, jsval value ){
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1204 sSMJS_Context* cs;
1205 Data_Get_Struct( context, sSMJS_Context, cs );
af5695f import from tar
nazoking authored Oct 3, 2006
1206
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1207 if( rbsm_lookup_jsval_to_rbval( cs, value ) ){
af5695f import from tar
nazoking authored Oct 3, 2006
1208 // 過去に同じ値でRubyオブジェクトを作成していた場合、そのオブジェクトを取り出す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1209 // When a past Ruby object was created at the same value, we
1210 // discard our half-constructed object, and use that instead.
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1211 return rbsm_get_jsval_to_rbval( cs, value );
af5695f import from tar
nazoking authored Oct 3, 2006
1212 }else{
121da36 @matthewd Defer creation of a new object until after we've determined that the
matthewd authored Jan 20, 2008
1213 sSMJS_Value* sv;
1214 VALUE self;
1215
1216 // 領域の確保 : "Guarantee of territory?"
1217 self = Data_Make_Struct( cJSValue, sSMJS_Value, 0, rb_smjs_value_free, sv );
1218 sv->cs = cs;
1219 sv->value = value;
1220 trace( "value_new(cx=%x, value=%x); [count %d -> %d]", sv->cs->cx, value, alloc_count_js2rb, ++alloc_count_js2rb );
1221
1222 // context をインスタンス変数として持つ
1223 // It has the context as an instance variable.
1224 rb_iv_set( self, RBSMJS_VALUES_CONTEXT, context );
1225
af5695f import from tar
nazoking authored Oct 3, 2006
1226 // 過去に作成していなかった場合
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1227 // If not, we store this object, and then use it.
af5695f import from tar
nazoking authored Oct 3, 2006
1228 rbsm_set_jsval_to_rbval( sv->cs, self, value );
1229 return self;
1230 }
1231 }
1232
1233 static VALUE
1234 rb_smjs_value_get_jsvalid( VALUE self ){
1235 sSMJS_Value* sv;
1236 jsid id;
1237
1238 Data_Get_Struct( self, sSMJS_Value, sv );
1239 JS_ValueToId( sv->cs->cx, sv->value, &id );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1240 return INT2NUM( id );
af5695f import from tar
nazoking authored Oct 3, 2006
1241 }
1242
1243 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1244 rb_smjs_value_get_context( VALUE self ){
1245 return rb_iv_get( self, RBSMJS_VALUES_CONTEXT );
af5695f import from tar
nazoking authored Oct 3, 2006
1246 }
1247
1248 // code を実行し、SpiderMonkey::Valueオブジェクトを返す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1249 // Execute code; a SpiderMonkey::Value is returned.
af5695f import from tar
nazoking authored Oct 3, 2006
1250 static VALUE
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
1251 rb_smjs_value_eval( int argc, VALUE* argv, VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
1252 sSMJS_Value* sv;
1253 Data_Get_Struct( self, sSMJS_Value, sv );
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
1254 return rb_smjs_convert_prim( sv->cs->cx, rb_smjs_evalscript( sv->cs, JSVAL_TO_OBJECT( sv->value ), argc, argv ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1255 }
1256
1257 // code を実行し、SpiderMonkey::Valueオブジェクトを返す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1258 // Execute code; a SpiderMonkey::Value is returned.
af5695f import from tar
nazoking authored Oct 3, 2006
1259 static VALUE
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
1260 rb_smjs_value_evalget( int argc, VALUE* argv, VALUE self ){
1261 return rb_smjs_value_new_jsval( rb_smjs_value_get_context( self ), rb_smjs_value_evalscript( argc, argv, self ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1262 }
1263
1264 // code を実行し、なるべく近いRuby のオブジェクトを返す
733a04d @matthewd Added some Google-powered translations of Japanese comments.
matthewd authored Jul 24, 2007
1265 // Execute code; where possible, the nearest Ruby object is returned.
af5695f import from tar
nazoking authored Oct 3, 2006
1266 static VALUE
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
1267 rb_smjs_value_evaluate( int argc, VALUE* argv, VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
1268 sSMJS_Value* sv;
1269 Data_Get_Struct( self, sSMJS_Value, sv );
122faa8 @matthewd Support optional filename and lineno arguments on the source evaluation
matthewd authored Jul 24, 2007
1270 return rb_smjs_convertvalue( sv->cs->cx, rb_smjs_evalscript( sv->cs, JSVAL_TO_OBJECT( sv->value ), argc, argv ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1271 }
1272 static VALUE
1273 rb_smjs_value_each( VALUE self ){
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1274 rbsm_value_each( self, rb_smjs_value_each_yield, NULL );
af5695f import from tar
nazoking authored Oct 3, 2006
1275 return Qnil;
1276 }
1277 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1278 rb_smjs_value_each_with_index( VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
1279 rbsm_value_each( self, rb_smjs_value_each_with_index_yield, NULL );
1280 return Qnil;
1281 }
1282
1283 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1284 rb_smjs_value_get_parent( VALUE self ){
1285 sSMJS_Value* sv;
af5695f import from tar
nazoking authored Oct 3, 2006
1286 JSObject* jo;
1287
1288 Data_Get_Struct( self, sSMJS_Value, sv );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1289 jo = JS_GetParent( sv->cs->cx, JSVAL_TO_OBJECT( sv->value ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1290 if( !jo ) return Qnil;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1291 return rb_smjs_convert_prim( sv->cs->cx, OBJECT_TO_JSVAL( jo ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1292 }
1293
1294 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1295 rb_smjs_value_get_prototype( VALUE self ){
1296 sSMJS_Value* sv;
af5695f import from tar
nazoking authored Oct 3, 2006
1297 JSObject* jo;
1298
1299 Data_Get_Struct( self, sSMJS_Value, sv );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1300 jo = JS_GetPrototype( sv->cs->cx, JSVAL_TO_OBJECT( sv->value ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1301 if( !jo ) return Qnil;
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1302 return rb_smjs_convert_prim( sv->cs->cx, OBJECT_TO_JSVAL( jo ) );
af5695f import from tar
nazoking authored Oct 3, 2006
1303 }
1304
1305 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1306 rb_smjs_value_call( VALUE self ){
1307 sSMJS_Value* sv;
af5695f import from tar
nazoking authored Oct 3, 2006
1308 Data_Get_Struct( self, sSMJS_Value, sv );
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1309 if( !JS_ObjectIsFunction( sv->cs->cx, JSVAL_TO_OBJECT( sv->value ) ) ){
1310 rb_raise( eJSEvalError, "object is not function" );
af5695f import from tar
nazoking authored Oct 3, 2006
1311 }
1312 return Qnil;
1313 }
1314
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1315 static VALUE RBSM_CALL_CONTEXT_JSVALUE( RBSMJS_Convert f, VALUE v ){
1316 sSMJS_Value* sv;
af5695f import from tar
nazoking authored Oct 3, 2006
1317 Data_Get_Struct( v, sSMJS_Value, sv );
1318 return f( sv->cs->cx, sv->value );
1319 }
1320
1321 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1322 rb_smjs_value_to_ruby( VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
1323 return RBSM_CALL_CONTEXT_JSVALUE( rb_smjs_convertvalue, self );
1324 }
1325
1326 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1327 rb_smjs_value_to_s( VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
1328 return RBSM_CALL_CONTEXT_JSVALUE( rb_smjs_to_s, self );
1329 }
1330
1331 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1332 rb_smjs_value_to_i( VALUE self ){
af5695f import from tar
nazoking authored Oct 3, 2006
1333 return RBSM_CALL_CONTEXT_JSVALUE( rb_smjs_to_i, self );
1334 }
1335
1336 static VALUE
5aca374 change space and brakeline and indent ( coding style )
nazoking authored Dec 10, 2006
1337 rb_smjs_value_to_f( VALUE self ){
af5695f