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

Commit

Permalink
Merge branch 'master' of git@github.com:jbarnette/johnson
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 8, 2008
2 parents 12d80b1 + 791ac06 commit 17c4395
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 70 deletions.
8 changes: 5 additions & 3 deletions Manifest.txt
Expand Up @@ -4,9 +4,6 @@ Manifest.txt
README.txt
Rakefile
bin/johnson
browser/env.js
browser/xmlsax.js
browser/xmlw3cdom.js
cross-compile.txt
ext/spidermonkey/context.c
ext/spidermonkey/context.h
Expand All @@ -31,6 +28,10 @@ ext/spidermonkey/ruby_land_proxy.c
ext/spidermonkey/ruby_land_proxy.h
ext/spidermonkey/spidermonkey.c
ext/spidermonkey/spidermonkey.h
js/johnson/browser.js
js/johnson/browser/env.js
js/johnson/browser/xmlsax.js
js/johnson/browser/xmlw3cdom.js
js/johnson/prelude.js
js/johnson/template.js
lib/johnson.rb
Expand Down Expand Up @@ -58,6 +59,7 @@ lib/johnson/visitors/dot_visitor.rb
lib/johnson/visitors/ecma_visitor.rb
lib/johnson/visitors/sexp_visitor.rb
test/helper.rb
test/johnson/browser_test.rb
test/johnson/context_test.rb
test/johnson/conversions/array_test.rb
test/johnson/conversions/boolean_test.rb
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -26,7 +26,7 @@ Hoe.new("johnson", Johnson::VERSION) do |p|

p.test_globs = ["test/**/*_test.rb"]

p.spec_extras = { :extensions => ["ext/spidermonkey/extconf.rb"] }
p.spec_extras = { :extensions => ["Rakefile"] }
end

namespace :test do
Expand All @@ -43,6 +43,7 @@ Rake::Task["test:todo"].prerequisites << :extensions
Rake::Task[:check_manifest].prerequisites << GENERATED_NODE

task :build => :extensions
task :extension => :build

# gem depends on the native extension actually building
Rake::Task[:gem].prerequisites << :extensions
Expand Down
12 changes: 6 additions & 6 deletions ext/spidermonkey/conversions.c
Expand Up @@ -18,7 +18,7 @@ static JSBool convert_float_or_bignum_to_js(OurContext* context, VALUE float_or_

static JSBool convert_symbol_to_js(OurContext* context, VALUE symbol, jsval* retval)
{
PREPARE_JROOTS(context, 2, 0);
PREPARE_JROOTS(context, 2);

VALUE to_s = CALL_RUBY_WRAPPER(rb_funcall_0, symbol, rb_intern("to_s"), 0);
jsval name = STRING_TO_JSVAL(JS_NewStringCopyN(context->js, StringValuePtr(to_s), (unsigned) StringValueLen(to_s)));
Expand Down Expand Up @@ -121,7 +121,7 @@ JSBool convert_to_js(OurContext* context, VALUE ruby, jsval* retval)

VALUE convert_jsstring_to_ruby(OurContext* context, JSString* str)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(str);
char* bytes = JS_GetStringBytes(str);
JCHECK(bytes);
Expand All @@ -130,7 +130,7 @@ VALUE convert_jsstring_to_ruby(OurContext* context, JSString* str)

static VALUE convert_regexp_to_ruby(OurContext* context, jsval regexp)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(regexp);
JSRegExp* re = (JSRegExp*)JS_GetPrivate(context->js, JSVAL_TO_OBJECT(regexp));

Expand All @@ -141,7 +141,7 @@ static VALUE convert_regexp_to_ruby(OurContext* context, jsval regexp)

static bool js_value_is_regexp(OurContext* context, jsval maybe_regexp)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);
JROOT(maybe_regexp);
JSBool result = JS_InstanceOf(context->js, JSVAL_TO_OBJECT(maybe_regexp), &js_RegExpClass, NULL);
JRETURN_RUBY(result ? true : false);
Expand All @@ -151,7 +151,7 @@ static bool js_value_is_symbol(OurContext* context, jsval maybe_symbol)
{
jsval nsJohnson, cSymbol;

PREPARE_RUBY_JROOTS(context, 3, 0);
PREPARE_RUBY_JROOTS(context, 3);
JROOT(maybe_symbol);

JCHECK(JS_GetProperty(context->js, context->global, "Johnson", &nsJohnson));
Expand All @@ -174,7 +174,7 @@ VALUE convert_to_ruby(OurContext* context, jsval js)
{
if (JSVAL_NULL == js) return Qnil;

PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);

switch (JS_TypeOfValue(context->js, js))
{
Expand Down
2 changes: 1 addition & 1 deletion ext/spidermonkey/extensions.c
Expand Up @@ -14,7 +14,7 @@ define_property(JSContext *js_context, JSObject* UNUSED(obj), uintN argc, jsval

VALUE init_spidermonkey_extensions(OurContext* context, VALUE self)
{
PREPARE_RUBY_JROOTS(context, 1, 0);
PREPARE_RUBY_JROOTS(context, 1);

jsval Object;
JCHECK(JS_GetProperty(context->js, context->global, "Object", &Object));
Expand Down
76 changes: 38 additions & 38 deletions ext/spidermonkey/jroot.h
Expand Up @@ -4,44 +4,29 @@
#define _JROOT_NAMESIZE 200
#define _JROOT_ERRSIZE 500

#define _JROOT_ROOT (void*)(1)

#define OUR_CONTEXT(js_context) \
({ \
OurContext* _context; \
VALUE _ruby_context = (VALUE)JS_GetContextPrivate(js_context); \
const OurContext* _context; \
const VALUE _ruby_context = (VALUE)JS_GetContextPrivate(js_context); \
Data_Get_Struct(_ruby_context, OurContext, _context); \
_context; \
})

#define _PREPARE_JROOTS(rb, context, rootcount, cleancount) \
#define _PREPARE_JROOTS(rb, context, cleancount) \
const bool _jroot_ruby = (rb); \
int _jroot_roots = (rootcount); \
void* _jroot_map[_jroot_roots]; \
int _jroot_cleans = (cleancount); \
const int _jroot_cleans = (cleancount); \
void (*_jroot_cleanup[_jroot_cleans])(OurContext*, void*); \
void* _jroot_cleanup_data[_jroot_cleans]; \
OurContext* _jroot_context = (context); \
int _jroot_cleanidx = 0; \
int _jroot_rootidx = 0

#define PREPARE_JROOTS(context, rootcount, cleancount) \
_PREPARE_JROOTS(false, context, rootcount, cleancount)

#define PREPARE_RUBY_JROOTS(context, rootcount, cleancount) \
_PREPARE_JROOTS(true, context, rootcount, cleancount)
int _jroot_cleanidx = 0;

#define _JROOT(ptr, name) \
do \
{ \
char _jroot_tmpname[_JROOT_NAMESIZE]; \
assert(_jroot_rootidx < _jroot_roots); \
_jroot_map[_jroot_rootidx] = (ptr); \
snprintf(_jroot_tmpname, _JROOT_NAMESIZE, "%s[%d]:%s: %s", __FILE__, __LINE__, __func__, (name)); \
JCHECK(JS_AddNamedRoot(_jroot_context->js, _jroot_map[_jroot_rootidx], _jroot_tmpname)); \
_jroot_rootidx++; \
} while(0)
#define PREPARE_JROOTS(context, cleancount) \
_PREPARE_JROOTS(false, context, cleancount)

#define JROOT(var) _JROOT(&(var), #var)
#define JROOT_PTR(ptr) _JROOT(ptr, #ptr)
#define PREPARE_RUBY_JROOTS(context, cleancount) \
_PREPARE_JROOTS(true, context, cleancount)

#define JCLEANUP(func, data) \
do \
Expand All @@ -52,30 +37,45 @@
_jroot_cleanidx++; \
} while(0)

#define _JROOT(ptr, name) \
do \
{ \
static char _name[_JROOT_NAMESIZE] = ""; \
void* const _root = (ptr); \
if (*_name == '\0') \
snprintf(_name, _JROOT_NAMESIZE, "%s[%d]:%s: %s", __FILE__, __LINE__, __func__, (name)); \
JCHECK(JS_AddNamedRoot(_jroot_context->js, _root, _name)); \
JCLEANUP(_JROOT_ROOT, _root); \
} while(0)

#define JROOT(var) _JROOT(&(var), #var)
#define JROOT_PTR(ptr) _JROOT(ptr, #ptr)

#define JUNROOT(var) \
do \
{ \
void* _jroot_match = &(var); \
void* const _jroot_match = &(var); \
int _jroot_i; \
for (_jroot_i = _jroot_rootidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_map[_jroot_i] == _jroot_match) \
for (_jroot_i = _jroot_cleanidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_cleanup[_jroot_i] == _JROOT_ROOT && _jroot_cleanup_data[_jroot_i] == _jroot_match) \
{ \
JS_RemoveRoot(_jroot_context->js, _jroot_map[_jroot_i]); \
if (_jroot_i == _jroot_rootidx - 1) _jroot_rootidx--; \
_jroot_map[_jroot_i] = NULL; \
JS_RemoveRoot(_jroot_context->js, _jroot_cleanup_data[_jroot_i]); \
if (_jroot_i == _jroot_cleanidx - 1) _jroot_cleanidx--; \
_jroot_cleanup[_jroot_i] = NULL; \
} \
} while (0)

#define REMOVE_JROOTS \
do \
{ \
int _jroot_i; \
for (_jroot_i = _jroot_rootidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_map[_jroot_i]) \
JS_RemoveRoot(_jroot_context->js, _jroot_map[_jroot_i]); \
for (_jroot_i = _jroot_cleanidx - 1; _jroot_i >= 0; _jroot_i--) \
if (_jroot_cleanup[_jroot_i]) \
{ \
if (_jroot_cleanup[_jroot_i] == _JROOT_ROOT) \
JS_RemoveRoot(_jroot_context->js, _jroot_cleanup_data[_jroot_i]); \
else if (_jroot_cleanup[_jroot_i]) \
(_jroot_cleanup[_jroot_i])(_jroot_context, _jroot_cleanup_data[_jroot_i]); \
} \
} while (0)

#define JCHECK(cond) \
Expand All @@ -94,8 +94,8 @@
#define JPROTECT(func, data) \
({ \
int _state; \
VALUE _old_errinfo = ruby_errinfo; \
VALUE _result = rb_protect((func), (data), &_state); \
const VALUE _old_errinfo = ruby_errinfo; \
const VALUE _result = rb_protect((func), (data), &_state); \
if (_state) \
{ \
REMOVE_JROOTS; \
Expand Down
20 changes: 10 additions & 10 deletions ext/spidermonkey/js_land_proxy.c
Expand Up @@ -196,7 +196,7 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 1, 0);
PREPARE_JROOTS(context, 1);
JROOT(id);

// get the Ruby object that backs this proxy
Expand Down Expand Up @@ -284,7 +284,7 @@ static JSBool get(JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
static JSBool get_and_destroy_resolved_property(
JSContext* js_context, JSObject* obj, jsval id, jsval* retval)
{
PREPARE_JROOTS(OUR_CONTEXT(js_context), 1, 0);
PREPARE_JROOTS(OUR_CONTEXT(js_context), 1);
JROOT(id);
char* name = JS_GetStringBytes(JSVAL_TO_STRING(id));
JCHECK(JS_DeleteProperty(js_context, obj, name));
Expand All @@ -299,7 +299,7 @@ static JSBool set(JSContext* js_context, JSObject* obj, jsval id, jsval* value)
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 2, 0);
PREPARE_JROOTS(context, 2);
JROOT(id);
JROOT_PTR(value);

Expand Down Expand Up @@ -365,7 +365,7 @@ static JSBool construct(JSContext* js_context, JSObject* UNUSED(obj), uintN argc
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE klass = CONVERT_TO_RUBY(context, JS_ARGV_CALLEE(argv));
VALUE args = rb_ary_new();
Expand All @@ -386,7 +386,7 @@ static JSBool resolve(JSContext *js_context, JSObject *obj, jsval id, uintN UNUS
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 1, 0);
PREPARE_JROOTS(context, 1);
JROOT(id);

char* name = JS_GetStringBytes(JS_ValueToString(js_context, id));
Expand All @@ -409,7 +409,7 @@ static JSBool to_string(JSContext* js_context, JSObject* obj, uintN UNUSED(argc)
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);

Expand All @@ -424,7 +424,7 @@ static JSBool to_array(JSContext* js_context, JSObject* obj, uintN UNUSED(argc),
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);

Expand All @@ -439,7 +439,7 @@ static JSBool method_missing(JSContext* js_context, JSObject* obj, uintN argc, j
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);

Expand All @@ -465,7 +465,7 @@ static JSBool call(JSContext* js_context, JSObject* UNUSED(obj), uintN argc, jsv
OurContext* context;
Data_Get_Struct(ruby_context, OurContext, context);

PREPARE_JROOTS(context, 0, 0);
PREPARE_JROOTS(context, 0);

VALUE self = (VALUE)JS_GetInstancePrivate(context->js, JSVAL_TO_OBJECT(JS_ARGV_CALLEE(argv)), &JSLandCallableProxyClass, NULL);

Expand Down Expand Up @@ -530,7 +530,7 @@ JSBool make_js_land_proxy(OurContext* context, VALUE value, jsval* retval)
}
else
{
PREPARE_JROOTS(context, 1, 0);
PREPARE_JROOTS(context, 1);

JSObject *jsobj;

Expand Down

0 comments on commit 17c4395

Please sign in to comment.