Permalink
Browse files

Resolve segfaults & warnings when sizeof(jsint) < sizeof(long).

  • Loading branch information...
1 parent 884e3a1 commit e7633854b24669f245867eb58632c489df309255 @matthewd matthewd committed Jul 27, 2008
View
4 ext/spidermonkey/context.c
@@ -48,7 +48,7 @@ initialize_native(VALUE self, VALUE rb_runtime, VALUE UNUSED(options))
Data_Get_Struct(self, JohnsonContext, context);
Data_Get_Struct(rb_runtime, JohnsonRuntime, runtime);
- if ((context->js = JS_NewContext(runtime->js, 8192)))
+ if ((context->js = JS_NewContext(runtime->js, 8192L)))
{
// See if the runtime already has a shared global object.
JSObject* global = runtime->global;
@@ -97,7 +97,7 @@ static void deallocate(JohnsonContext *context) {
static VALUE allocate(VALUE klass)
{
- JohnsonContext* context = calloc(1, sizeof(JohnsonContext));
+ JohnsonContext* context = calloc(1L, sizeof(JohnsonContext));
return Data_Wrap_Struct(klass, 0, deallocate, context);
}
View
2 ext/spidermonkey/context.h
@@ -3,7 +3,7 @@
#include "spidermonkey.h"
-#define MAX_EXCEPTION_MESSAGE_SIZE 2048
+#define MAX_EXCEPTION_MESSAGE_SIZE 2048L
typedef struct {
JSContext *js;
View
67 ext/spidermonkey/conversions.c
@@ -22,7 +22,7 @@ static JSBool convert_symbol_to_js(JohnsonRuntime* runtime, VALUE symbol, jsval*
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, StringValuePtr(to_s), (unsigned) StringValueLen(to_s)));
+ jsval name = STRING_TO_JSVAL(JS_NewStringCopyN(context, StringValuePtr(to_s), (size_t) StringValueLen(to_s)));
JROOT(name);
@@ -40,60 +40,59 @@ static JSBool convert_symbol_to_js(JohnsonRuntime* runtime, VALUE symbol, jsval*
static JSBool convert_regexp_to_js(JohnsonRuntime* runtime, VALUE regexp, jsval* retval)
{
JSContext * context = johnson_get_current_context(runtime);
+ PREPARE_JROOTS(context, 0);
VALUE source = rb_funcall(regexp, rb_intern("source"), 0);
- int options = NUM2INT(rb_funcall(regexp, rb_intern("options"), 0));
+ jsint options = (jsint)(NUM2INT(rb_funcall(regexp, rb_intern("options"), 0)));
JSObject* obj = JS_NewRegExpObject(context,
StringValuePtr(source),
- (unsigned) StringValueLen(source),
+ (size_t) StringValueLen(source),
(unsigned) options);
- if (obj) {
- *retval = OBJECT_TO_JSVAL(obj);
- return JS_TRUE;
- } else {
- return JS_FALSE;
- }
+ JCHECK(obj);
+ *retval = OBJECT_TO_JSVAL(obj);
+ JRETURN;
}
JSBool convert_to_js(JohnsonRuntime* runtime, VALUE ruby, jsval* retval)
{
JSContext * context = johnson_get_current_context(runtime);
+ PREPARE_JROOTS(context, 0);
switch(TYPE(ruby))
{
case T_NIL:
*retval = JSVAL_NULL;
- return JS_TRUE;
+ JRETURN;
case T_TRUE:
*retval = JSVAL_TRUE;
- return JS_TRUE;
+ JRETURN;
case T_FALSE:
*retval = JSVAL_FALSE;
- return JS_TRUE;
+ JRETURN;
case T_STRING:
{
- JSString* str = JS_NewStringCopyN(context, StringValuePtr(ruby), (unsigned) StringValueLen(ruby));
- if (str) {
- *retval = STRING_TO_JSVAL(str);
- return JS_TRUE;
- } else {
- return JS_FALSE;
- }
+ JSString* str = JS_NewStringCopyN(context, StringValuePtr(ruby), (size_t) StringValueLen(ruby));
+ JCHECK(str);
+ *retval = STRING_TO_JSVAL(str);
+ JRETURN;
}
case T_FIXNUM:
- *retval = INT_TO_JSVAL(NUM2INT(ruby));
- return JS_TRUE;
+ // FIXME: Not all T_FIXNUM will fit in a jsint ( == int32)
+ *retval = INT_TO_JSVAL((jsint)(NUM2INT(ruby)));
+ JRETURN;
case T_FLOAT:
case T_BIGNUM:
- return convert_float_or_bignum_to_js(runtime, ruby, retval);
+ JCHECK(convert_float_or_bignum_to_js(runtime, ruby, retval));
+ JRETURN;
case T_SYMBOL:
- return convert_symbol_to_js(runtime, ruby, retval);
+ JCHECK(convert_symbol_to_js(runtime, ruby, retval));
+ JRETURN;
case T_CLASS:
case T_ARRAY:
@@ -102,24 +101,26 @@ JSBool convert_to_js(JohnsonRuntime* runtime, VALUE ruby, jsval* retval)
case T_FILE:
case T_STRUCT:
case T_OBJECT:
- return make_js_land_proxy(runtime, ruby, retval);
+ JCHECK(make_js_land_proxy(runtime, ruby, retval));
+ JRETURN;
case T_REGEXP:
- return convert_regexp_to_js(runtime, ruby, retval);
+ JCHECK(convert_regexp_to_js(runtime, ruby, retval));
+ JRETURN;
case T_DATA: // HEY! keep T_DATA last for fall-through
if (ruby_value_is_proxy(ruby))
- return unwrap_ruby_land_proxy(runtime, ruby, retval);
+ JCHECK(unwrap_ruby_land_proxy(runtime, ruby, retval));
+ else // If we can't identify the object, just wrap it
+ JCHECK(make_js_land_proxy(runtime, ruby, retval));
+ JRETURN;
- // If we can't identify the object, just wrap it
- return make_js_land_proxy(runtime, ruby, retval);
-
default:
- rb_raise(rb_eRuntimeError, "unknown ruby type in switch");
+ JERROR("unknown ruby type in switch");
}
*retval = JSVAL_NULL;
- return JS_TRUE;
+ JRETURN;
}
VALUE convert_js_string_to_ruby(JohnsonRuntime* runtime, JSString* str)
@@ -129,7 +130,7 @@ VALUE convert_js_string_to_ruby(JohnsonRuntime* runtime, JSString* str)
JROOT(str);
char* bytes = JS_GetStringBytes(str);
JCHECK(bytes);
- JRETURN_RUBY(rb_str_new(bytes, (signed)JS_GetStringLength(str)));
+ JRETURN_RUBY(rb_str_new(bytes, (signed long)JS_GetStringLength(str)));
}
static VALUE convert_regexp_to_ruby(JohnsonRuntime* runtime, jsval regexp)
@@ -141,7 +142,7 @@ static VALUE convert_regexp_to_ruby(JohnsonRuntime* runtime, jsval regexp)
JRETURN_RUBY(CALL_RUBY_WRAPPER(rb_funcall_2, rb_cRegexp, rb_intern("new"), 2,
convert_js_string_to_ruby(runtime, re->source),
- INT2NUM(re->flags)));
+ INT2NUM((long)re->flags)));
}
static bool js_value_is_regexp(JohnsonRuntime* runtime, jsval maybe_regexp)
View
10 ext/spidermonkey/debugger.c
@@ -22,7 +22,7 @@ static VALUE line_number(VALUE UNUSED(self), VALUE context, VALUE script, VALUE
Data_Get_Struct(script, JSScript, js_script);
Data_Get_Struct(bytecode, jsbytecode, js_bytecode);
- return INT2NUM(JS_PCToLineNumber(js, js_script, js_bytecode));
+ return INT2NUM((long)JS_PCToLineNumber(js, js_script, js_bytecode));
}
static VALUE file_name(VALUE UNUSED(self), VALUE context, VALUE script)
@@ -64,7 +64,7 @@ static void new_script_hook(JSContext *UNUSED(js),
{
VALUE self = (VALUE)rb;
VALUE rb_filename = rb_str_new2(filename);
- VALUE rb_linenum = UINT2NUM(lineno);
+ VALUE rb_linenum = UINT2NUM((unsigned long)lineno);
/* FIXME: Pass the rest of this crap to the debugger? */
rb_funcall(self, rb_intern("new_script_hook"), 2, rb_filename, rb_linenum);
@@ -103,8 +103,8 @@ static void source_handler(const char *filename, uintN lineno,
{
VALUE self = (VALUE)rb;
VALUE rb_filename = rb_str_new2(filename);
- VALUE rb_lineno = UINT2NUM(lineno);
- VALUE rb_str = rb_str_new((char *)str, (signed)(length * sizeof(jschar)));
+ VALUE rb_lineno = ULONG2NUM((unsigned long)lineno);
+ VALUE rb_str = rb_str_new((char *)str, (signed long)(length * sizeof(jschar)));
rb_funcall(self, rb_intern("source_handler"), 3, rb_filename, rb_lineno, rb_str);
}
@@ -164,7 +164,7 @@ static JSBool debug_error_hook(JSContext *UNUSED(js), const char *message,
static VALUE allocate(VALUE klass)
{
- JSDebugHooks* debug = calloc(1, sizeof(JSDebugHooks));
+ JSDebugHooks* debug = calloc(1L, sizeof(JSDebugHooks));
VALUE self = Data_Wrap_Struct(klass, 0, 0, debug);
debug->interruptHandler = interrupt_handler;
View
22 ext/spidermonkey/immutable_node.c.erb
@@ -17,12 +17,12 @@ static void deallocate(ImmutableNodeContext* context)
static VALUE allocate(VALUE klass)
{
- ImmutableNodeContext * context = calloc(1, sizeof(ImmutableNodeContext));
+ ImmutableNodeContext * context = calloc(1L, sizeof(ImmutableNodeContext));
VALUE self = Data_Wrap_Struct(klass, 0, deallocate, context);
assert(context->runtime = JS_NewRuntime(0x100000));
- assert(context->js = JS_NewContext(context->runtime, 8192));
+ assert(context->js = JS_NewContext(context->runtime, 8192L));
return self;
}
@@ -40,7 +40,7 @@ static VALUE parse_io(int argc, VALUE *argv, VALUE klass) {
ImmutableNodeContext* context;
Data_Get_Struct(self, ImmutableNodeContext, context);
- assert(context->pc = calloc(1, sizeof(JSParseContext)));
+ assert(context->pc = calloc(1L, sizeof(JSParseContext)));
rb_scan_args( argc, argv, "12", &stream, &filename, &linenum );
@@ -80,7 +80,7 @@ static VALUE parse_io(int argc, VALUE *argv, VALUE klass) {
Qnil :
rb_str_new2(JS_GetStringBytes(JSVAL_TO_STRING(message))),
rb_str_new2(JS_GetStringBytes(JSVAL_TO_STRING(file_name))),
- INT2NUM(JSVAL_TO_INT(line_number))
+ INT2NUM((long)JSVAL_TO_INT(line_number))
);
}
@@ -97,7 +97,7 @@ static VALUE line(VALUE self) {
ImmutableNodeContext * ctx;
Data_Get_Struct(self, ImmutableNodeContext, ctx);
- return INT2NUM(ctx->node->pn_pos.begin.lineno);
+ return INT2NUM((long)(ctx->node->pn_pos.begin.lineno));
}
/*
@@ -110,7 +110,7 @@ static VALUE begin_index(VALUE self) {
ImmutableNodeContext * ctx;
Data_Get_Struct(self, ImmutableNodeContext, ctx);
- return INT2NUM(ctx->node->pn_pos.begin.index);
+ return INT2NUM((long)(ctx->node->pn_pos.begin.index));
}
/*
@@ -157,7 +157,7 @@ static VALUE pn_type(VALUE self) {
case <%= token %>: return ID2SYM(rb_intern("<%= token.downcase %>"));
<% end %>
}
- return INT2NUM(ctx->node->pn_type);
+ return INT2NUM((long)(ctx->node->pn_type));
}
/*
@@ -273,7 +273,7 @@ static VALUE data_pn_dval(VALUE self) {
if(JSVAL_IS_NUMBER(ATOM_KEY(ctx->node->pn_atom))) {
return rb_float_new(ctx->node->pn_dval);
} else {
- return INT2NUM((int)(ctx->node->pn_dval));
+ return INT2NUM((long)(ctx->node->pn_dval));
}
}
@@ -322,7 +322,7 @@ static VALUE data_pn_extra(VALUE self) {
Data_Get_Struct(self, ImmutableNodeContext, ctx);
- return UINT2NUM(ctx->node->pn_extra);
+ return UINT2NUM((unsigned long)(ctx->node->pn_extra));
}
/*
@@ -393,7 +393,7 @@ static VALUE function_args(VALUE self) {
object = ctx->node->pn_funpob->object;
f = (JSFunction *)JS_GetPrivate(ctx->js, ctx->node->pn_funpob->object);
- func_args = rb_ary_new2(f->nargs);
+ func_args = rb_ary_new2((long)f->nargs);
if(f->nargs > 0) {
names = js_GetLocalNameArray(ctx->js, f, &ctx->js->tempPool);
for(i = 0; i < f->nargs; i++) {
@@ -484,7 +484,7 @@ VALUE jsop_to_symbol(JSUint32 jsop)
case <%= jsop %>: return ID2SYM(rb_intern("<%= jsop.downcase %>"));
<% end %>
}
- return UINT2NUM(jsop);
+ return UINT2NUM((unsigned long)(jsop));
}
void init_Johnson_SpiderMonkey_Immutable_Node(VALUE spidermonkey)
View
8 ext/spidermonkey/jroot.h
@@ -1,8 +1,8 @@
#ifndef JOHNSON_JROOT_H
#define JOHNSON_JROOT_H
-#define _JROOT_NAMESIZE 200
-#define _JROOT_ERRSIZE 500
+#define _JROOT_NAMESIZE 200L
+#define _JROOT_ERRSIZE 500L
#define _JROOT_ROOT (void*)(1)
@@ -176,10 +176,10 @@
#define DEFINE_RUBY_WRAPPER(name, func, arglist) \
VALUE name ## _invoke(VALUE magic) \
{ \
- name ## _args * _data = (name ## _args *)(FIX2INT(magic) << 2); \
+ name ## _args * _data = (name ## _args *)(FIX2LONG(magic) << 2); \
return func(arglist); \
}
-#define RUBY_WRAPPER_ARG(name, args...) ({ name ## _args _x = { args }; INT2FIX((int)(&_x) >> 2); })
+#define RUBY_WRAPPER_ARG(name, args...) ({ name ## _args _x = { args }; LONG2FIX((long)(&_x) >> 2); })
#define RUBY_WRAPPER(name) name ## _invoke
#define CALL_RUBY_WRAPPER(name, args...) JPROTECT(RUBY_WRAPPER(name), RUBY_WRAPPER_ARG(name, args))
View
11 ext/spidermonkey/js_land_proxy.c
@@ -61,14 +61,14 @@ static VALUE call_ruby_from_js_invoke(VALUE args)
JSBool call_ruby_from_js_va(JohnsonRuntime* runtime, VALUE* result, VALUE self, ID id, int argc, va_list va)
{
VALUE old_errinfo = ruby_errinfo;
- VALUE args = rb_ary_new2(argc + 2);
+ VALUE args = rb_ary_new2((long)argc + 2);
- int i;
+ long i;
for(i = 0; i < argc; i++)
rb_ary_store(args, i, va_arg(va, VALUE));
- rb_ary_store(args, argc, ID2SYM(id));
- rb_ary_store(args, argc + 1, self);
+ rb_ary_store(args, (long)argc, ID2SYM(id));
+ rb_ary_store(args, (long)argc + 1, self);
int state;
*result = rb_protect(call_ruby_from_js_invoke, args, &state);
@@ -181,8 +181,9 @@ static bool respond_to_p(JSContext* js_context, JSObject* obj, char* name)
static jsval evaluate_js_property_expression(JohnsonRuntime * runtime, const char * property, jsval* retval) {
JSContext * context = johnson_get_current_context(runtime);
+ assert(strlen(property) < INT_MAX);
return JS_EvaluateScript(context, runtime->global,
- property, strlen(property), "johnson:evaluate_js_property_expression", 1,
+ property, (unsigned int)strlen(property), "johnson:evaluate_js_property_expression", 1,
retval);
}
View
6 ext/spidermonkey/ruby_land_proxy.c
@@ -65,7 +65,7 @@ get(VALUE self, VALUE name)
switch(TYPE(name)) {
case T_FIXNUM:
JCHECK(JS_GetElement(context,
- JSVAL_TO_OBJECT(proxy_value), NUM2INT(name), &js_value));
+ JSVAL_TO_OBJECT(proxy_value), (jsint)(NUM2INT(name)), &js_value));
break;
default:
Check_Type(name, T_STRING);
@@ -104,7 +104,7 @@ set(VALUE self, VALUE name, VALUE value)
switch(TYPE(name)) {
case T_FIXNUM:
JCHECK(JS_SetElement(context,
- JSVAL_TO_OBJECT(proxy_value), NUM2INT(name), &js_value));
+ JSVAL_TO_OBJECT(proxy_value), (jsint)(NUM2INT(name)), &js_value));
break;
default:
Check_Type(name, T_STRING);
@@ -279,7 +279,7 @@ each(VALUE self)
VALUE key = CONVERT_TO_RUBY(proxy->runtime, js_key);
VALUE value = CONVERT_TO_RUBY(proxy->runtime, js_value);
- CALL_RUBY_WRAPPER(rb_yield, rb_ary_new3(2, key, value));
+ CALL_RUBY_WRAPPER(rb_yield, rb_ary_new3(2L, key, value));
JUNROOT(js_value);
JUNROOT(js_key);
View
2 ext/spidermonkey/runtime.c
@@ -284,7 +284,7 @@ static void deallocate(JohnsonRuntime* runtime)
static VALUE allocate(VALUE klass)
{
- JohnsonRuntime* runtime = calloc(1, sizeof(JohnsonRuntime));
+ JohnsonRuntime* runtime = calloc(1L, sizeof(JohnsonRuntime));
return Data_Wrap_Struct(klass, 0, deallocate, runtime);
}

0 comments on commit e763385

Please sign in to comment.