码迷,mamicode.com
首页 > 其他好文 > 详细

spidermonkey simple demo

时间:2015-02-28 20:23:06      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

#define STATIC_JS_API 1
#ifdef JS_THREADSAFE
#undef JS_THREADSAFE
#endif
#include <iostream>
#include "jsapi.h"

using namespace JS;


/* spidermonkey
1.runtime
2.context
3.global object
*/


static JSClass globalClass = 
{
	"global",
    JSCLASS_GLOBAL_FLAGS,
    JS_PropertyStub,
    JS_DeletePropertyStub,
    JS_PropertyStub,
    JS_StrictPropertyStub,
    JS_EnumerateStub,
    JS_ResolveStub,
    JS_ConvertStub,
    nullptr,nullptr,nullptr,nullptr,
    nullptr,//JS_GlobalObjectTraceHook
};

/*/---------------
static JSFunctionSpec myjs_global_functions[] = {
    JS_FS("rand",   myjs_rand,   0, 0),
    JS_FS("srand",  myjs_srand,  0, 0),
    JS_FS("system", myjs_system, 1, 0),
    JS_FS_END
};
*///------------------------------

// The error reporter callback.
void reportError(JSContext *cx, const char *message, JSErrorReport *report) {
     fprintf(stderr, "%s:%u:%s\n",
             report->filename ? report->filename : "[no filename]",
             (unsigned int) report->lineno,
             message);
}


int run(JSContext *cx) {
    // Enter a request before running anything in the context.
    JSAutoRequest ar(cx);

    // Create the global object and a new compartment.
    RootedObject global(cx);
    global = JS_NewGlobalObject(cx, &globalClass, nullptr,
                                JS::DontFireOnNewGlobalHook);
    if (!global)
        return 1;

    // Enter the new global object‘s compartment.
    JSAutoCompartment ac(cx, global);

    // Populate the global object with the standard globals, like Object and
    // Array.
    if (!JS_InitStandardClasses(cx, global))
        return 1;

    // Your application code here. This may include JSAPI calls to create your
    // own custom JS objects and run scripts.
#if 1
    char* script = "var today = Date(); today.toString();";
    jsval rval;
    uint lineno = 0;
    bool ok = JS_EvaluateScript(
    		cx, 
            global, 		// The scope in which to execute the script. This parameter is 						// documented in detail at JS_ExecuteScript. 
            script,
            strlen(script), 
            
            //---------------
            "script", 		// Name of file or URL containing the script. Used to report filename or 			// URL in error messages.
            lineno, 		// Line number. Used to report the offending line in the file or URL if 			// an error occurs.
            //---------------
            &rval			// Out parameter. On success, if rval is not NULL, *rval receives the 				// result value.
            );
                             
	JSString* str = JS_ValueToSource(cx, rval);
   	size_t str_len = JS_GetStringLength(str);
    
    const jschar* p = JS_GetStringCharsAndLength(cx,str,&str_len);
	char*pp = JS_EncodeString(cx,str);
    fprintf(stderr,"%s\n",pp);
    JS_free(cx,pp);
#endif
    //free(pp);
    
            	    return 0;
}


int main(int argc, const char *argv[]) {
    // Initialize the JS engine.
    if (!JS_Init())
       return 1;

    // Create a JS runtime.
    JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L,JS_USE_HELPER_THREADS);
    if (!rt)
       return 1;

    // Create a context.
    JSContext *cx = JS_NewContext(rt, 8192);
    if (!cx)
       return 1;
    JS_SetErrorReporter(cx, &reportError);

    int status = run(cx);

    // Shut everything down.
    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);
    JS_ShutDown();

    return status;
}



spidermonkey simple demo

标签:

原文地址:http://my.oschina.net/lyr/blog/380882

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!