Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Moved our global object creation, etc into a separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarnette committed Apr 23, 2008
1 parent 443a663 commit 0dc6129
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 63 deletions.
2 changes: 2 additions & 0 deletions Manifest.txt
Expand Up @@ -12,6 +12,8 @@ ext/spidermonkey/error.h
ext/spidermonkey/extconf.rb
ext/spidermonkey/extensions.c
ext/spidermonkey/extensions.h
ext/spidermonkey/global.c
ext/spidermonkey/global.h
ext/spidermonkey/idhash.c
ext/spidermonkey/idhash.h
ext/spidermonkey/immutable_node.c
Expand Down
102 changes: 39 additions & 63 deletions ext/spidermonkey/context.c
@@ -1,51 +1,20 @@
#include "context.h"
#include "conversions.h"
#include "global.h"
#include "error.h"
#include "extensions.h"
#include "idhash.h"

static JSBool define_property(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *retval);

static JSBool global_enumerate(JSContext *js_context, JSObject *obj)
{
return JS_EnumerateStandardClasses(js_context, obj);
}

static JSBool global_resolve(
JSContext *js_context, JSObject *obj, jsval id, uintN flags, JSObject **objp)
{
if ((flags & JSRESOLVE_ASSIGNING) == 0) {
JSBool resolved_p;

if (!JS_ResolveStandardClass(js_context, obj, id, &resolved_p))
return JS_FALSE;
if (resolved_p) *objp = obj;
}

return JS_TRUE;
}

static JSClass OurGlobalClass = {
"global", JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, // addProperty
JS_PropertyStub, // delProperty
JS_PropertyStub, // getProperty
JS_PropertyStub, // setProperty
global_enumerate,
(JSResolveOp) global_resolve,
JS_ConvertStub,
JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};

static VALUE global(VALUE self)
static VALUE /* Context#global */
global(VALUE self)
{
OurContext* context;
Data_Get_Struct(self, OurContext, context);
return convert_to_ruby(context, OBJECT_TO_JSVAL(context->global));
}

static VALUE evaluate(int argc, VALUE* argv, VALUE self)
static VALUE /* Context#evaluate(script, filename=nil, linenum=nil) */
evaluate(int argc, VALUE* argv, VALUE self)
{
VALUE script, filename, linenum;

Expand Down Expand Up @@ -87,7 +56,8 @@ static VALUE evaluate(int argc, VALUE* argv, VALUE self)
return convert_to_ruby(context, js);
}

static void error(JSContext* js, const char* message, JSErrorReport* report)
// callback for JS_SetErrorReporter
static void report_js_error(JSContext* js, const char* message, JSErrorReport* report)
{
// first we find ourselves
VALUE self = (VALUE)JS_GetContextPrivate(js);
Expand All @@ -105,31 +75,9 @@ static void error(JSContext* js, const char* message, JSErrorReport* report)
JS_GetPendingException(context->js, &context->ex);
}

static void deallocate(OurContext* context)
{
JS_SetContextPrivate(context->js, 0);

JS_RemoveRoot(context->js, &(context->global));
JS_RemoveRoot(context->js, &(context->gcthings));
JS_HashTableDestroy(context->rbids);
JS_HashTableDestroy(context->jsids);

context->jsids = 0;
context->rbids = 0;

JS_DestroyContext(context->js);
JS_DestroyRuntime(context->runtime);

free(context);
}

static VALUE allocate(VALUE klass)
/* private */ static VALUE /* Context#initialize_native(options={}) */
initialize_native(VALUE self, VALUE options)
{
OurContext* context = calloc(1, sizeof(OurContext));
return Data_Wrap_Struct(klass, 0, deallocate, context);
}

static VALUE initialize_native(VALUE self, VALUE options) {
OurContext* context;
bool gthings_rooted_p = false;

Expand All @@ -141,10 +89,10 @@ static VALUE initialize_native(VALUE self, VALUE options) {
&& (context->rbids = create_id_hash())
&& (context->gcthings = JS_NewObject(context->js, NULL, 0, 0))
&& (gthings_rooted_p = JS_AddNamedRoot(context->js, &(context->gcthings), "context->gcthings"))
&& (context->global = JS_NewObject(context->js, &OurGlobalClass, NULL, NULL))
&& (context->global = create_global_object(context))
&& (JS_AddNamedRoot(context->js, &(context->global), "context->global")))
{
JS_SetErrorReporter(context->js, error);
JS_SetErrorReporter(context->js, report_js_error);
JS_SetContextPrivate(context->js, (void *)self);

if (init_spidermonkey_extensions(context))
Expand All @@ -171,6 +119,34 @@ static VALUE initialize_native(VALUE self, VALUE options) {
rb_raise(rb_eRuntimeError, "Failed to initialize SpiderMonkey context");
}

///////////////////////////////////////////////////////////////////////////
//// INFRASTRUCTURE BELOW HERE ////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

static void deallocate(OurContext* context)
{
JS_SetContextPrivate(context->js, 0);

JS_RemoveRoot(context->js, &(context->global));
JS_RemoveRoot(context->js, &(context->gcthings));
JS_HashTableDestroy(context->rbids);
JS_HashTableDestroy(context->jsids);

context->jsids = 0;
context->rbids = 0;

JS_DestroyContext(context->js);
JS_DestroyRuntime(context->runtime);

free(context);
}

static VALUE allocate(VALUE klass)
{
OurContext* context = calloc(1, sizeof(OurContext));
return Data_Wrap_Struct(klass, 0, deallocate, context);
}

void init_Johnson_SpiderMonkey_Context(VALUE spidermonkey)
{
VALUE context = rb_define_class_under(spidermonkey, "Context", rb_cObject);
Expand Down
40 changes: 40 additions & 0 deletions ext/spidermonkey/global.c
@@ -0,0 +1,40 @@
#include "global.h"

static JSBool enumerate(JSContext *js_context, JSObject *obj)
{
return JS_EnumerateStandardClasses(js_context, obj);
}

static JSBool resolve(JSContext *js_context, JSObject *obj, jsval id, uintN flags, JSObject **objp)
{
if ((flags & JSRESOLVE_ASSIGNING) == 0)
{
JSBool resolved_p;

if (!JS_ResolveStandardClass(js_context, obj, id, &resolved_p))
return JS_FALSE;

if (resolved_p)
*objp = obj;
}

return JS_TRUE;
}

static JSClass OurGlobalClass = {
"global", JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, // addProperty
JS_PropertyStub, // delProperty
JS_PropertyStub, // getProperty
JS_PropertyStub, // setProperty
enumerate,
(JSResolveOp) resolve,
JS_ConvertStub,
JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};

JSObject* create_global_object(OurContext* context)
{
JS_NewObject(context->js, &OurGlobalClass, NULL, NULL);
}
9 changes: 9 additions & 0 deletions ext/spidermonkey/global.h
@@ -0,0 +1,9 @@
#ifndef JOHNSON_SPIDERMONKEY_GLOBAL_H
#define JOHNSON_SPIDERMONKEY_GLOBAL_H

#include "spidermonkey.h"
#include "context.h"

JSObject* create_global_object(OurContext* context);

#endif

0 comments on commit 0dc6129

Please sign in to comment.