Basic program seg-faults

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Basic program seg-faults

Mike Diehl
Hi all,

I'm new to Spidermonkey and am working through the examples to see if I can get something simple working.  However, when I run the resulting program, it seg-faults.  I've identified the line of code that causes it, but I can't see what I'm doing wrong.  Here is the code:

JSRuntime *rt;
JSContext *cx;
JSObject *global;

static JSClass global_class = {
        "global",
        JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
        JS_PropertyStub,
        JS_DeletePropertyStub,
        JS_PropertyStub,
        JS_StrictPropertyStub,
        JS_EnumerateStub,
        JS_ResolveStub,
        JS_ConvertStub,
        nullptr,
        JSCLASS_NO_OPTIONAL_MEMBERS
};

void    init () {

        rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
        if (!rt) return;

        cx = JS_NewContext(rt, 8192);
        if (!cx) return;

        global = JS_NewGlobalObject(cx, &global_class, nullptr);
        if (!global) return;

        JS_SetGlobalObject(cx, global);

// xxx This line causes a seg-fault
        if (!JS_InitStandardClasses(cx, global))
        return;

        jsval *rval;
char src[] = "alert(\'hello\');";
        JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);
}



Any ideas?

Thank you,

Mike.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Basic program seg-faults

Tim-2
Possibly your global class definition is wrong? that has changed several times, depending on what version of spidermonkey you are running.

On 28/01/14 16:37, Mike Diehl wrote:

> Hi all,
>
> I'm new to Spidermonkey and am working through the examples to see if I can get something simple working.  However, when I run the resulting program, it seg-faults.  I've identified the line of code that causes it, but I can't see what I'm doing wrong.  Here is the code:
>
> JSRuntime *rt;
> JSContext *cx;
> JSObject *global;
>
> static JSClass global_class = {
>         "global",
>         JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
>         JS_PropertyStub,
>         JS_DeletePropertyStub,
>         JS_PropertyStub,
>         JS_StrictPropertyStub,
>         JS_EnumerateStub,
>         JS_ResolveStub,
>         JS_ConvertStub,
>         nullptr,
>         JSCLASS_NO_OPTIONAL_MEMBERS
> };
>
> void    init () {
>
>         rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
>         if (!rt) return;
>
>         cx = JS_NewContext(rt, 8192);
>         if (!cx) return;
>
>         global = JS_NewGlobalObject(cx, &global_class, nullptr);
>         if (!global) return;
>
>         JS_SetGlobalObject(cx, global);
>
> // xxx This line causes a seg-fault
>         if (!JS_InitStandardClasses(cx, global))
>         return;
>
>         jsval *rval;
> char src[] = "alert(\'hello\');";
>         JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);
> }
>
>
>
> Any ideas?
>
> Thank you,
>
> Mike.
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Basic program seg-faults

Mike Diehl
In reply to this post by Mike Diehl
On Tuesday, January 28, 2014 12:37:15 AM UTC-5, Mike Diehl wrote:

> Hi all,
>
>
>
> I'm new to Spidermonkey and am working through the examples to see if I can get something simple working.  However, when I run the resulting program, it seg-faults.  I've identified the line of code that causes it, but I can't see what I'm doing wrong.  Here is the code:
>
>
>
> JSRuntime *rt;
>
> JSContext *cx;
>
> JSObject *global;
>
>
>
> static JSClass global_class = {
>
>         "global",
>
>         JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
>
>         JS_PropertyStub,
>
>         JS_DeletePropertyStub,
>
>         JS_PropertyStub,
>
>         JS_StrictPropertyStub,
>
>         JS_EnumerateStub,
>
>         JS_ResolveStub,
>
>         JS_ConvertStub,
>
>         nullptr,
>
>         JSCLASS_NO_OPTIONAL_MEMBERS
>
> };
>
>
>
> void    init () {
>
>
>
>         rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
>
>         if (!rt) return;
>
>
>
>         cx = JS_NewContext(rt, 8192);
>
>         if (!cx) return;
>
>
>
>         global = JS_NewGlobalObject(cx, &global_class, nullptr);
>
>         if (!global) return;
>
>
>
>         JS_SetGlobalObject(cx, global);
>
>
>
> // xxx This line causes a seg-fault
>
>         if (!JS_InitStandardClasses(cx, global))
>
>         return;
>
>
>
>         jsval *rval;
>
> char src[] = "alert(\'hello\');";
>
>         JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);
>
> }
>
>
>
>
>
>
>
> Any ideas?
>
>
>
> Thank you,
>
>
>
> Mike.

Tim,

After reading your reply, I went and took the definition of global_class right out of mozjs-24.2.0/js/src/shell/js.cpp.  Still seg-faults.

Mike.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Basic program seg-faults

Tim-2
for mozjs24 you will need to enter a compartment before running anything that accesses JS objects. Something like this should help:

    JSAutoCompartment ac(context, global);

    JS_SetGlobalObject(context, global);

    if (!JS_InitStandardClasses(context, global))

        return FALSE;


On 28/01/14 19:01, Mike Diehl wrote:

> On Tuesday, January 28, 2014 12:37:15 AM UTC-5, Mike Diehl wrote:
>> Hi all,
>>
>>
>>
>> I'm new to Spidermonkey and am working through the examples to see if I can get something simple working.  However, when I run the resulting program, it seg-faults.  I've identified the line of code that causes it, but I can't see what I'm doing wrong.  Here is the code:
>>
>>
>>
>> JSRuntime *rt;
>>
>> JSContext *cx;
>>
>> JSObject *global;
>>
>>
>>
>> static JSClass global_class = {
>>
>>         "global",
>>
>>         JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
>>
>>         JS_PropertyStub,
>>
>>         JS_DeletePropertyStub,
>>
>>         JS_PropertyStub,
>>
>>         JS_StrictPropertyStub,
>>
>>         JS_EnumerateStub,
>>
>>         JS_ResolveStub,
>>
>>         JS_ConvertStub,
>>
>>         nullptr,
>>
>>         JSCLASS_NO_OPTIONAL_MEMBERS
>>
>> };
>>
>>
>>
>> void    init () {
>>
>>
>>
>>         rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
>>
>>         if (!rt) return;
>>
>>
>>
>>         cx = JS_NewContext(rt, 8192);
>>
>>         if (!cx) return;
>>
>>
>>
>>         global = JS_NewGlobalObject(cx, &global_class, nullptr);
>>
>>         if (!global) return;
>>
>>
>>
>>         JS_SetGlobalObject(cx, global);
>>
>>
>>
>> // xxx This line causes a seg-fault
>>
>>         if (!JS_InitStandardClasses(cx, global))
>>
>>         return;
>>
>>
>>
>>         jsval *rval;
>>
>> char src[] = "alert(\'hello\');";
>>
>>         JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);
>>
>> }
>>
>>
>>
>>
>>
>>
>>
>> Any ideas?
>>
>>
>>
>> Thank you,
>>
>>
>>
>> Mike.
> Tim,
>
> After reading your reply, I went and took the definition of global_class right out of mozjs-24.2.0/js/src/shell/js.cpp.  Still seg-faults.
>
> Mike.
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Basic program seg-faults

Mike Diehl
In reply to this post by Mike Diehl
On Tuesday, January 28, 2014 12:37:15 AM UTC-5, Mike Diehl wrote:

> Hi all,
>
>
>
> I'm new to Spidermonkey and am working through the examples to see if I can get something simple working.  However, when I run the resulting program, it seg-faults.  I've identified the line of code that causes it, but I can't see what I'm doing wrong.  Here is the code:
>
>
>
> JSRuntime *rt;
>
> JSContext *cx;
>
> JSObject *global;
>
>
>
> static JSClass global_class = {
>
>         "global",
>
>         JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
>
>         JS_PropertyStub,
>
>         JS_DeletePropertyStub,
>
>         JS_PropertyStub,
>
>         JS_StrictPropertyStub,
>
>         JS_EnumerateStub,
>
>         JS_ResolveStub,
>
>         JS_ConvertStub,
>
>         nullptr,
>
>         JSCLASS_NO_OPTIONAL_MEMBERS
>
> };
>
>
>
> void    init () {
>
>
>
>         rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
>
>         if (!rt) return;
>
>
>
>         cx = JS_NewContext(rt, 8192);
>
>         if (!cx) return;
>
>
>
>         global = JS_NewGlobalObject(cx, &global_class, nullptr);
>
>         if (!global) return;
>
>
>
>         JS_SetGlobalObject(cx, global);
>
>
>
> // xxx This line causes a seg-fault
>
>         if (!JS_InitStandardClasses(cx, global))
>
>         return;
>
>
>
>         jsval *rval;
>
> char src[] = "alert(\'hello\');";
>
>         JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);
>
> }
>
>
>
>
>
>
>
> Any ideas?
>
>
>
> Thank you,
>
>
>
> Mike.

OK, that seems to have fixed it.

However, I was hoping that my program would "do" something when it got to:

jsval *rval;
char src[] = "var a = \'hello\'; alert(a);";
JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);


Even if I got a javascript error, I'd be happy!  I guess I'll have to create a binding for alert(), first?

Should I expect any output at this point?

Mike.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Basic program seg-faults

Mike Diehl
In reply to this post by Mike Diehl
On Tuesday, January 28, 2014 12:37:15 AM UTC-5, Mike Diehl wrote:

> Hi all,
>
>
>
> I'm new to Spidermonkey and am working through the examples to see if I can get something simple working.  However, when I run the resulting program, it seg-faults.  I've identified the line of code that causes it, but I can't see what I'm doing wrong.  Here is the code:
>
>
>
> JSRuntime *rt;
>
> JSContext *cx;
>
> JSObject *global;
>
>
>
> static JSClass global_class = {
>
>         "global",
>
>         JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
>
>         JS_PropertyStub,
>
>         JS_DeletePropertyStub,
>
>         JS_PropertyStub,
>
>         JS_StrictPropertyStub,
>
>         JS_EnumerateStub,
>
>         JS_ResolveStub,
>
>         JS_ConvertStub,
>
>         nullptr,
>
>         JSCLASS_NO_OPTIONAL_MEMBERS
>
> };
>
>
>
> void    init () {
>
>
>
>         rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
>
>         if (!rt) return;
>
>
>
>         cx = JS_NewContext(rt, 8192);
>
>         if (!cx) return;
>
>
>
>         global = JS_NewGlobalObject(cx, &global_class, nullptr);
>
>         if (!global) return;
>
>
>
>         JS_SetGlobalObject(cx, global);
>
>
>
> // xxx This line causes a seg-fault
>
>         if (!JS_InitStandardClasses(cx, global))
>
>         return;
>
>
>
>         jsval *rval;
>
> char src[] = "alert(\'hello\');";
>
>         JSBool r = JS_EvaluateScript(cx, global, src, strlen(src), "test.js", 1, rval);
>
> }
>
>
>
>
>
>
>
> Any ideas?
>
>
>
> Thank you,
>
>
>
> Mike.

Well, once I supplied an error reporter function, I got some output.  I'm good to go.  Thank you for your help.

Mike.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine