Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compatibility with spidermonkey 1.8.5 #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions c_src/to_erl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#define OK 1
#define ERROR 0

#ifndef JSID_VOID
#include <js/jsapi.h>
#define JSID_VOID JSVAL_VOID
#endif



int to_erl_intern(ErlNifEnv* env, JSContext* cx, jsval val, ERL_NIF_TERM* term);

int
Expand All @@ -26,7 +33,7 @@ to_erl_string(ErlNifEnv* env, JSContext* cx, jsval val, ERL_NIF_TERM* term)
size_t len;

str = JS_ValueToString(cx, val);
data = JS_GetStringBytesZ(cx, str);
data = JS_EncodeString(cx, str);
if(data == NULL) return ERROR;
len = strlen(data);

Expand Down Expand Up @@ -106,7 +113,7 @@ to_erl_object(ErlNifEnv* env, JSContext* cx, JSObject* obj, ERL_NIF_TERM* term)
length = 0;
while(JS_NextProperty(cx, iter, &idp))
{
if(idp == JSVAL_VOID) break;
if(idp == JSID_VOID) break;
length += 1;
}

Expand All @@ -119,7 +126,7 @@ to_erl_object(ErlNifEnv* env, JSContext* cx, JSObject* obj, ERL_NIF_TERM* term)
index = 0;
while(JS_NextProperty(cx, iter, &idp))
{
if(idp == JSVAL_VOID)
if(idp == JSID_VOID)
{
list = enif_make_list_from_array(env, array, length);
*term = enif_make_tuple1(env, list);
Expand Down
65 changes: 62 additions & 3 deletions c_src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "util.h"
#include "vm.h"


typedef enum
{
job_unknown,
Expand Down Expand Up @@ -45,20 +46,26 @@ struct vm_t
int alive;
};


static JSClass global_class = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
#ifdef JS185
JS_StrictPropertyStub,
#else
JS_PropertyStub,
#endif
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};


void* vm_run(void* arg);
ENTERM vm_eval(JSContext* cx, JSObject* gl, job_ptr job);
ENTERM vm_call(JSContext* cx, JSObject* gl, job_ptr job);
Expand Down Expand Up @@ -109,21 +116,27 @@ static JSClass jserl_class = {
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
#ifdef JS185
JS_StrictPropertyStub,
#else
JS_PropertyStub,
#endif
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};

#ifdef JS185
static JSBool
jserl_send(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)
jserl_send(JSContext* cx, uintN argc, jsval* vp)
{
vm_ptr vm = (vm_ptr) JS_GetContextPrivate(cx);
ErlNifEnv* env;
job_ptr job;
ENTERM mesg;
jsval* argv = JS_ARGV(cx, vp);

if(argc < 0)
{
Expand Down Expand Up @@ -153,12 +166,54 @@ jserl_send(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)
}

assert(job->type == job_response && "Invalid message response.");
JS_SET_RVAL(cx, vp, to_js(job->env, cx, job->args));
job_destroy(job);

return JS_TRUE;
}
#else
static JSBool
jserl_send(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)
{
vm_ptr vm = (vm_ptr) JS_GetContextPrivate(cx);
ErlNifEnv* env;
job_ptr job;
ENTERM mesg;

if(argc < 0)
{
return JS_FALSE;
}

assert(vm != NULL && "Context has no vm.");

env = enif_alloc_env();
mesg = vm_mk_message(env, to_erl(env, cx, argv[0]));

// If pid is not alive, raise an error.
// XXX: Can I make this uncatchable?
if(!enif_send(NULL, &(vm->curr_job->pid), env, mesg))
{
JS_ReportError(cx, "Context closing.");
return JS_FALSE;
}

job = queue_receive(vm->jobs);
if(job->type == job_close)
{
// XXX: Can I make this uncatchable?
job_destroy(job);
JS_ReportError(cx, "Context closing.");
return JS_FALSE;
}

assert(job->type == job_response && "Invalid message response.");
*rval = to_js(job->env, cx, job->args);
job_destroy(job);

return JS_TRUE;
}
#endif

int
install_jserl(JSContext* cx, JSObject* gl)
Expand Down Expand Up @@ -257,8 +312,12 @@ vm_run(void* arg)
flags |= JSOPTION_COMPILE_N_GO;
flags |= JSOPTION_XML;
JS_SetOptions(cx, JS_GetOptions(cx) | flags);


#ifdef JS185
gl = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
#else
gl = JS_NewObject(cx, &global_class, NULL, NULL);
#endif
if(gl == NULL)
{
fprintf(stderr, "Failed to create global object.\n");
Expand Down Expand Up @@ -430,7 +489,7 @@ vm_call(JSContext* cx, JSObject* gl, job_ptr job)
jsval rval;
jsid idp;
int argc;

// Get the function object.

func = to_js(job->env, cx, job->name);
Expand Down
5 changes: 5 additions & 0 deletions c_src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

#include "alias.h"


#ifdef JS_NewCompartmentAndGlobalObject
#define JS185 1
#endif

typedef struct vm_t* vm_ptr;

vm_ptr vm_init(ErlNifResourceType* res_type, JSRuntime* runtime, size_t stack_size);
Expand Down
2 changes: 1 addition & 1 deletion test/004-basic-calls.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env escript
%%! -pa ./test/ -pa ./ebin/
%%! -smp +K true -pa ./test/ -pa ./ebin/
% This file is part of Emonk released under the MIT license.
% See the LICENSE file for more information.

Expand Down
2 changes: 1 addition & 1 deletion test/005-erl-to-js.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env escript
%%! -pa ./test/ -pa ./ebin/
%%! -smp +K true -pa ./test/ -pa ./ebin/
% This file is part of Emonk released under the MIT license.
% See the LICENSE file for more information.

Expand Down
2 changes: 1 addition & 1 deletion test/006-js-to-erl.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env escript
%%! -pa ./test/ -pa ./ebin/
%%! -smp +K true -pa ./test/ -pa ./ebin/
% This file is part of Emonk released under the MIT license.
% See the LICENSE file for more information.

Expand Down
2 changes: 1 addition & 1 deletion test/007-js-send-message.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env escript
%%! -pa ./test/ -pa ./ebin/
%%! -smp +K true -pa ./test/ -pa ./ebin/
% This file is part of Emonk released under the MIT license.
% See the LICENSE file for more information.

Expand Down