diff CppApplication_1/first.cpp @ 1:4ac33e06cb09

This netbeans project for single thread application it didn't work yet, may be the spidermonkey's version is the reason and I'll try to solve this
author Sara
date Sun, 05 Jan 2014 13:43:45 -0800
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/CppApplication_1/first.cpp	Sun Jan 05 13:43:45 2014 -0800
     1.3 @@ -0,0 +1,176 @@
     1.4 +/* 
     1.5 + * File:   first.cpp
     1.6 + * Author: sara
     1.7 + *
     1.8 + * Created on December 30, 2013, 11:41 AM
     1.9 + */
    1.10 +
    1.11 +#include <cstdlib>
    1.12 +/* Include the JSAPI header file to get access to SpiderMonkey. */
    1.13 +#include "jsapi.h"
    1.14 +#include <pthread.h>
    1.15 +
    1.16 +using namespace std;
    1.17 +struct thread_parameter
    1.18 +{
    1.19 + // char *uncompiledScript;
    1.20 +   // JSObject *sharedGlobalObject;
    1.21 +   // JSRuntime *currRt;
    1.22 +    char *tempstr;
    1.23 +   
    1.24 +
    1.25 +};
    1.26 +/* The class of the global object. */
    1.27 +JSRuntime *rt;
    1.28 +JSObject  *global;
    1.29 +static JSClass global_class = {
    1.30 +    "global", JSCLASS_GLOBAL_FLAGS,
    1.31 +    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
    1.32 +    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
    1.33 +    JSCLASS_NO_OPTIONAL_MEMBERS
    1.34 +};
    1.35 +
    1.36 +/* The error reporter callback. */
    1.37 +void reportError(JSContext *cx, const char *message, JSErrorReport *report)
    1.38 +{
    1.39 +    fprintf(stderr, "%s:%u:%s\n",
    1.40 +            report->filename ? report->filename : "<no filename=\"filename\">",
    1.41 +            (unsigned int) report->lineno,
    1.42 +            message);
    1.43 +}
    1.44 +/** the function which the thread executes when is created at main 
    1.45 + *@parameter structure of all the argument the function needs
    1.46 + *@return return null at the end 
    1.47 +*/ 
    1.48 +void *threadFunction(void* arg){
    1.49 + /* indicates that the execution of the script is executed successfully or not*/
    1.50 + bool execute;
    1.51 + int temp = 1;
    1.52 +// char *script = "'Hi' + 'Sara'";
    1.53 + char threadScript[] = "'Hi ' + 'Sara!'";
    1.54 + char *scr = threadScript;
    1.55 +  JSString *outStr;
    1.56 +/* carries the value of the last expression of the script*/
    1.57 + jsval rval1;
    1.58 + 
    1.59 +/* The new context which is created by the thread*/
    1.60 +  JSContext *currContext;
    1.61 +/* pass the incoming argument of the function */
    1.62 +// struct thread_parameter *arg1 = (struct thread_parameter *) arg;
    1.63 +//const char *uncScript = (const char*)arg->uncompiledScript;
    1.64 +  //scr = (char *)arg;
    1.65 +/*Creates new context to be attached to the creator thread*/ 
    1.66 +currContext = JS_NewContext(rt, 8192);
    1.67 +  if (currContext == NULL)
    1.68 +       return NULL;
    1.69 +/* In a thread safe build every entry to the api that uses
    1.70 +   context must be in a request for thread safety*/
    1.71 +  JS_BeginRequest(currContext);
    1.72 +   JS_SetOptions(currContext, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
    1.73 +   JS_SetVersion(currContext, JSVERSION_LATEST);
    1.74 +/*Compiles the incoming script*/
    1.75 +  JSObject *compiledScript = JS_CompileScript(currContext, global, scr,strlen(scr), "threadFilename", 0);
    1.76 +/*Executes the compiled script*/
    1.77 +   execute = JS_ExecuteScript(currContext, global, compiledScript, &rval1);
    1.78 +      if ( rval1 == JS_FALSE)//rval == JS_NULL |
    1.79 +        return NULL;
    1.80 +    /*gets the returned value of the last expression of the executed script*/
    1.81 +    outStr = JS_ValueToString(currContext, rval1);
    1.82 +    printf("%s\n", JS_EncodeString(currContext, outStr));
    1.83 +/*Ends the request */
    1.84 +    JS_EndRequest(currContext);
    1.85 +/*Destroy the context of that thread*/
    1.86 +     JS_DestroyContext(currContext);
    1.87 + //printf("HI, Sara");
    1.88 +    return NULL;
    1.89 +    
    1.90 +}
    1.91 +/*
    1.92 + * 
    1.93 + */
    1.94 +int main(int argc, const char *argv[]) {
    1.95 +
    1.96 +     /* JSAPI variables. */
    1.97 +    //JSRuntime *rt;
    1.98 +    JSContext *cx;
    1.99 +   // JSObject  *global;
   1.100 +/* id of the created thread*/
   1.101 +    pthread_t thread_id;
   1.102 +
   1.103 +    /* Create a JS runtime. You always need at least one runtime per process. */
   1.104 +    rt = JS_NewRuntime(8 * 1024 * 1024);
   1.105 +    if (rt == NULL)
   1.106 +        return 1;
   1.107 +
   1.108 +    /* 
   1.109 +     * Create a context. You always need a context per thread.
   1.110 +     * Note that this program is not multi-threaded.
   1.111 +     This thread is used by the main thread only
   1.112 +     */
   1.113 +    cx = JS_NewContext(rt, 8192);
   1.114 +    if (cx == NULL)
   1.115 +        return 1;
   1.116 +    JS_BeginRequest(cx);
   1.117 +    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
   1.118 +    JS_SetVersion(cx, JSVERSION_LATEST);
   1.119 +    JS_SetErrorReporter(cx, reportError);
   1.120 +
   1.121 +    /*
   1.122 +     * Create the global object in a new compartment.
   1.123 +     * You always need a global object per context.
   1.124 +     */
   1.125 +    
   1.126 +    global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
   1.127 +    if (global == NULL)
   1.128 +        return 1;
   1.129 +
   1.130 +    /*
   1.131 +     * Populate the global object with the standard JavaScript
   1.132 +     * function and object classes, such as Object, Array, Date.
   1.133 +     */
   1.134 +    if (!JS_InitStandardClasses(cx, global))
   1.135 +        return 1;
   1.136 +
   1.137 +    /* Your application code here. This may include JSAPI calls
   1.138 +     * to create your own custom JavaScript objects and to run scripts.
   1.139 +     *
   1.140 +     * The following example code creates a literal JavaScript script,
   1.141 +     * evaluates it, and prints the result to stdout.
   1.142 +     *
   1.143 +     * Errors are conventionally saved in a JSBool variable named ok.
   1.144 +     */
   1.145 +
   1.146 +
   1.147 +    /** Create the thread here....*/
   1.148 +    struct thread_parameter threadArg;
   1.149 +    //threadArg.sharedGlobalObject = global;
   1.150 +     char threadScript[] = "'Hi ' + 'Sara!'";
   1.151 +    //threadArg.tempstr = threadScript;
   1.152 +    //threadArg.currRt = rt;
   1.153 +   
   1.154 +    pthread_create(&thread_id, NULL, &threadFunction,NULL);
   1.155 +/*****************************************************************/
   1.156 +    const char *script = "'Hello ' + 'World!'";
   1.157 +    jsval rval;
   1.158 +    JSString *str;
   1.159 +    JSBool ok;
   1.160 +    const char *filename = "noname";
   1.161 +    uintN lineno = 0;
   1.162 +    ok = JS_EvaluateScript(cx, global, script, strlen(script),
   1.163 +                           filename, lineno, &rval);
   1.164 +    if ( rval == JS_FALSE)//rval == JS_NULL |
   1.165 +        return 1;
   1.166 +    str = JS_ValueToString(cx, rval);
   1.167 +    printf("%s\n", JS_EncodeString(cx, str));
   1.168 +
   1.169 +    /* End of your application code */
   1.170 +
   1.171 +    /* Clean things up and shut down SpiderMonkey. */
   1.172 +    JS_EndRequest(cx);
   1.173 +    JS_DestroyContext(cx);
   1.174 +    JS_DestroyRuntime(rt);
   1.175 +    JS_ShutDown();
   1.176 +    
   1.177 +    return 0;
   1.178 +}
   1.179 +