Skip to content
Browse files

Flush with JS_ClearScope; keep a single global object.

I'm not entirely clear on why, but this appears to avoid the problem
with conflicting Array (etc) classes caused by replacing the global
object on flush.
  • Loading branch information...
1 parent c70f4aa commit e3fdcffce4837020ee7c25f2c63a36a777240f15 @matthewd committed Dec 7, 2010
Showing with 13 additions and 5 deletions.
  1. +13 −5 spidermonkey.c
View
18 spidermonkey.c
@@ -1912,6 +1912,7 @@ rb_smjs_context_initialize( int argc, VALUE* argv, VALUE self ){
sSMJS_Context* cs;
int stacksize;
int options;
+ JSObject* jsGlobal;
Data_Get_Struct( self, sSMJS_Context, cs );
strncpy( cs->last_message, "<<NO MESSAGE>>", BUFSIZ );
@@ -1962,6 +1963,14 @@ rb_smjs_context_initialize( int argc, VALUE* argv, VALUE self ){
// Register the error report handler
JS_SetErrorReporter( cs->cx, rb_smjs_context_errorhandle );
+
+ // Initialize the global JavaScript object
+ jsGlobal = JS_NewObject( cs->cx, &global_class, 0, 0 );
+ if( ! jsGlobal )
+ rb_raise( eJSError, "Failed to create global object" );
+ JS_SetGlobalObject( cs->cx, jsGlobal );
+
+
rb_smjs_context_flush( self );
return Qnil;
@@ -1976,14 +1985,13 @@ rb_smjs_context_flush( VALUE self ){
char* str_newDate = "return new Date(arguments[0] * 1000);";
Data_Get_Struct( self, sSMJS_Context, cs );
- // Initialize the global JavaScript object
- jsGlobal = JS_NewObject( cs->cx, &global_class, 0, 0 );
- if( ! jsGlobal )
- rb_raise( eJSError, "Failed to create global object" );
+ jsGlobal = JS_GetGlobalObject( cs->cx );
JS_AddNamedRoot( cs->cx, &jsGlobal, "rb_smjs_context_flush" );
+
+ JS_ClearScope( cs->cx, jsGlobal );
if( !JS_InitStandardClasses( cs->cx, jsGlobal ) )
rb_raise( eJSError, "Failed to initialize global object" );
-
+
JS_CompileFunction( cs->cx, jsGlobal, "__getStack__", 0, NULL, str_getStack, strlen( str_getStack ), "spidermonkey.c:str_getStack", 1 );
JS_CompileFunction( cs->cx, jsGlobal, "__newDate__", 0, NULL, str_newDate, strlen( str_newDate ), "spidermonkey.c:str_newDate", 1 );

0 comments on commit e3fdcff

Please sign in to comment.
Something went wrong with that request. Please try again.