annotate SpiderMonkey_tests/Test1/first.cpp @ 6:c82791ba5ce1

added two-context test, from botton of pdf, plus pdf
author Sean Halle <seanhalle@yahoo.com>
date Sat, 01 Feb 2014 14:26:10 -0800
parents
children
rev   line source
seanhalle@6 1 /*
seanhalle@6 2 * File: first.cpp
seanhalle@6 3 * Author: sara
seanhalle@6 4 *
seanhalle@6 5 * Created on December 30, 2013, 11:41 AM
seanhalle@6 6 */
seanhalle@6 7
seanhalle@6 8 #include <cstdlib>
seanhalle@6 9 /* Include the JSAPI header file to get access to SpiderMonkey. */
seanhalle@6 10 #include "jsapi.h"
seanhalle@6 11 #include <pthread.h>
seanhalle@6 12
seanhalle@6 13 using namespace std;
seanhalle@6 14 struct thread_parameter
seanhalle@6 15 {
seanhalle@6 16 // char *uncompiledScript;
seanhalle@6 17 // JSObject *sharedGlobalObject;
seanhalle@6 18 // JSRuntime *currRt;
seanhalle@6 19 char *tempstr;
seanhalle@6 20
seanhalle@6 21
seanhalle@6 22 };
seanhalle@6 23 /* The class of the global object. */
seanhalle@6 24 JSRuntime *rt;
seanhalle@6 25 JSObject *global;
seanhalle@6 26 static JSClass global_class = {
seanhalle@6 27 "global", JSCLASS_GLOBAL_FLAGS,
seanhalle@6 28 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
seanhalle@6 29 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
seanhalle@6 30 JSCLASS_NO_OPTIONAL_MEMBERS
seanhalle@6 31 };
seanhalle@6 32
seanhalle@6 33 /* The error reporter callback. */
seanhalle@6 34 void reportError(JSContext *cx, const char *message, JSErrorReport *report)
seanhalle@6 35 {
seanhalle@6 36 fprintf(stderr, "%s:%u:%s\n",
seanhalle@6 37 report->filename ? report->filename : "<no filename=\"filename\">",
seanhalle@6 38 (unsigned int) report->lineno,
seanhalle@6 39 message);
seanhalle@6 40 }
seanhalle@6 41 /** the function which the thread executes when is created at main
seanhalle@6 42 *@parameter structure of all the argument the function needs
seanhalle@6 43 *@return return null at the end
seanhalle@6 44 */
seanhalle@6 45 void *threadFunction(void* arg){
seanhalle@6 46 /* indicates that the execution of the script is executed successfully or not*/
seanhalle@6 47 bool execute;
seanhalle@6 48 int temp = 1;
seanhalle@6 49 // char *script = "'Hi' + 'Sara'";
seanhalle@6 50 char threadScript[] = "'Hi ' + 'Sara!'";
seanhalle@6 51 char *scr = threadScript;
seanhalle@6 52 JSString *outStr;
seanhalle@6 53 /* carries the value of the last expression of the script*/
seanhalle@6 54 jsval rval1;
seanhalle@6 55
seanhalle@6 56 /* The new context which is created by the thread*/
seanhalle@6 57 JSContext *currContext;
seanhalle@6 58 /* pass the incoming argument of the function */
seanhalle@6 59 // struct thread_parameter *arg1 = (struct thread_parameter *) arg;
seanhalle@6 60 //const char *uncScript = (const char*)arg->uncompiledScript;
seanhalle@6 61 //scr = (char *)arg;
seanhalle@6 62 /*Creates new context to be attached to the creator thread*/
seanhalle@6 63 currContext = JS_NewContext(rt, 8192);
seanhalle@6 64 if (currContext == NULL)
seanhalle@6 65 return NULL;
seanhalle@6 66 /* In a thread safe build every entry to the api that uses
seanhalle@6 67 context must be in a request for thread safety*/
seanhalle@6 68 JS_BeginRequest(currContext);
seanhalle@6 69 JS_SetOptions(currContext, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
seanhalle@6 70 JS_SetVersion(currContext, JSVERSION_LATEST);
seanhalle@6 71 /*Compiles the incoming script*/
seanhalle@6 72 JSObject *compiledScript = JS_CompileScript(currContext, global, scr,strlen(scr), "threadFilename", 0);
seanhalle@6 73 /*Executes the compiled script*/
seanhalle@6 74 execute = JS_ExecuteScript(currContext, global, compiledScript, &rval1);
seanhalle@6 75 if ( rval1 == JS_FALSE)//rval == JS_NULL |
seanhalle@6 76 return NULL;
seanhalle@6 77 /*gets the returned value of the last expression of the executed script*/
seanhalle@6 78 outStr = JS_ValueToString(currContext, rval1);
seanhalle@6 79 printf("%s\n", JS_EncodeString(currContext, outStr));
seanhalle@6 80 /*Ends the request */
seanhalle@6 81 JS_EndRequest(currContext);
seanhalle@6 82 /*Destroy the context of that thread*/
seanhalle@6 83 JS_DestroyContext(currContext);
seanhalle@6 84 //printf("HI, Sara");
seanhalle@6 85 return NULL;
seanhalle@6 86
seanhalle@6 87 }
seanhalle@6 88 /*
seanhalle@6 89 *
seanhalle@6 90 */
seanhalle@6 91 int main(int argc, const char *argv[]) {
seanhalle@6 92
seanhalle@6 93 /* JSAPI variables. */
seanhalle@6 94 //JSRuntime *rt;
seanhalle@6 95 JSContext *cx;
seanhalle@6 96 // JSObject *global;
seanhalle@6 97 /* id of the created thread*/
seanhalle@6 98 pthread_t thread_id;
seanhalle@6 99
seanhalle@6 100 /* Create a JS runtime. You always need at least one runtime per process. */
seanhalle@6 101 rt = JS_NewRuntime(8 * 1024 * 1024);
seanhalle@6 102 if (rt == NULL)
seanhalle@6 103 return 1;
seanhalle@6 104
seanhalle@6 105 /*
seanhalle@6 106 * Create a context. You always need a context per thread.
seanhalle@6 107 * Note that this program is not multi-threaded.
seanhalle@6 108 This thread is used by the main thread only
seanhalle@6 109 */
seanhalle@6 110 cx = JS_NewContext(rt, 8192);
seanhalle@6 111 if (cx == NULL)
seanhalle@6 112 return 1;
seanhalle@6 113 JS_BeginRequest(cx);
seanhalle@6 114 JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
seanhalle@6 115 JS_SetVersion(cx, JSVERSION_LATEST);
seanhalle@6 116 JS_SetErrorReporter(cx, reportError);
seanhalle@6 117
seanhalle@6 118 /*
seanhalle@6 119 * Create the global object in a new compartment.
seanhalle@6 120 * You always need a global object per context.
seanhalle@6 121 */
seanhalle@6 122
seanhalle@6 123 global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
seanhalle@6 124 if (global == NULL)
seanhalle@6 125 return 1;
seanhalle@6 126
seanhalle@6 127 /*
seanhalle@6 128 * Populate the global object with the standard JavaScript
seanhalle@6 129 * function and object classes, such as Object, Array, Date.
seanhalle@6 130 */
seanhalle@6 131 if (!JS_InitStandardClasses(cx, global))
seanhalle@6 132 return 1;
seanhalle@6 133
seanhalle@6 134 /* Your application code here. This may include JSAPI calls
seanhalle@6 135 * to create your own custom JavaScript objects and to run scripts.
seanhalle@6 136 *
seanhalle@6 137 * The following example code creates a literal JavaScript script,
seanhalle@6 138 * evaluates it, and prints the result to stdout.
seanhalle@6 139 *
seanhalle@6 140 * Errors are conventionally saved in a JSBool variable named ok.
seanhalle@6 141 */
seanhalle@6 142
seanhalle@6 143
seanhalle@6 144 /** Create the thread here....*/
seanhalle@6 145 struct thread_parameter threadArg;
seanhalle@6 146 //threadArg.sharedGlobalObject = global;
seanhalle@6 147 char threadScript[] = "'Hi ' + 'Sara!'";
seanhalle@6 148 //threadArg.tempstr = threadScript;
seanhalle@6 149 //threadArg.currRt = rt;
seanhalle@6 150
seanhalle@6 151 pthread_create(&thread_id, NULL, &threadFunction,NULL);
seanhalle@6 152 /*****************************************************************/
seanhalle@6 153 const char *script = "'Hello ' + 'World!'";
seanhalle@6 154 jsval rval;
seanhalle@6 155 JSString *str;
seanhalle@6 156 JSBool ok;
seanhalle@6 157 const char *filename = "noname";
seanhalle@6 158 uintN lineno = 0;
seanhalle@6 159 ok = JS_EvaluateScript(cx, global, script, strlen(script),
seanhalle@6 160 filename, lineno, &rval);
seanhalle@6 161 if ( rval == JS_FALSE)//rval == JS_NULL |
seanhalle@6 162 return 1;
seanhalle@6 163 str = JS_ValueToString(cx, rval);
seanhalle@6 164 printf("%s\n", JS_EncodeString(cx, str));
seanhalle@6 165
seanhalle@6 166 /* End of your application code */
seanhalle@6 167
seanhalle@6 168 /* Clean things up and shut down SpiderMonkey. */
seanhalle@6 169 JS_EndRequest(cx);
seanhalle@6 170 JS_DestroyContext(cx);
seanhalle@6 171 JS_DestroyRuntime(rt);
seanhalle@6 172 JS_ShutDown();
seanhalle@6 173
seanhalle@6 174 return 0;
seanhalle@6 175 }
seanhalle@6 176