SpiderMonkey Simple Demo

Source: Internet
Author: User

#define STATIC_JS_API 1#ifdef js_threadsafe#undef js_threadsafe#endif#include <iostream> #include "jsapi.h" Using namespace js;/* spidermonkey1.runtime2.context3.global object*/static jsclass globalclass = {"Global", JSCLASS_GL Obal_flags, Js_propertystub, Js_deletepropertystub, Js_propertystub, Js_strictpropertystub, JS_EnumerateStu B, 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", my Js_srand, 0, 0), Js_fs ("System", Myjs_system, 1, 0), js_fs_end};*///------------------------------//The error repo Rter 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::D ontfireonnewglobalhook);    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 could 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 are//documented in detail at Js_executescript. Script, StrleN (script),//---------------"script",//Name of the 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 was 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.