Created
May 5, 2009 16:54
-
-
Save erh/107056 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sm.cpp | |
#define XP_UNIX | |
#include "mozjs/jsapi.h" | |
#include <string> | |
#include <string.h> | |
#include <assert.h> | |
#include <malloc.h> | |
/* The class of the global object. */ | |
static JSClass global_class = { | |
"global", JSCLASS_GLOBAL_FLAGS, | |
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
JSCLASS_NO_OPTIONAL_MEMBERS | |
}; | |
/* 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); | |
} | |
JSBool silly_constructor( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval ){ | |
jsval x; | |
JS_NewNumberValue( cx , 3 , &x ); | |
JS_SetProperty( cx , obj , "x" , &x ); | |
*rval = OBJECT_TO_JSVAL(obj); | |
return JS_TRUE; | |
} | |
JSClass silly_class = { | |
"Silly" , 0 , | |
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
JS_EnumerateStub, JS_ResolveStub , JS_ConvertStub, JS_FinalizeStub, | |
0 , 0 , 0 , | |
silly_constructor , | |
0 , 0 , 0 , 0 | |
}; | |
JSBool silly_func(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ | |
jsval x; | |
assert( JS_NewNumberValue( cx , 4 , &x ) ); | |
*rval = x; | |
} | |
JSFunctionSpec silly_functions[] = { | |
{ "y" , silly_func , 0 , 0 , 0 } , | |
{ 0 } | |
}; | |
JSBool native_print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ | |
assert( argc == 1 ); | |
JSString * so = JS_ValueToString( cx , argv[0] ); | |
jschar * s = JS_GetStringChars( so ); | |
size_t srclen = JS_GetStringLength( so ); | |
size_t len = srclen * 2; | |
char * dst = (char*)malloc( len ); | |
assert( JS_EncodeCharacters( cx , s , srclen , dst , &len) ); | |
dst[len] = 0; | |
printf( "%s\n" , dst ); | |
free( dst ); | |
return JS_TRUE; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
/* JS variables. */ | |
JSRuntime *rt; | |
JSContext *cx; | |
JSObject *global; | |
/* Create a JS runtime. */ | |
rt = JS_NewRuntime(8L * 1024L * 1024L); | |
if (rt == NULL) | |
return 1; | |
/* Create a context. */ | |
cx = JS_NewContext(rt, 8192); | |
if (cx == NULL) | |
return 1; | |
JS_SetOptions(cx, JSOPTION_VAROBJFIX); | |
//JS_SetVersion(cx, JSVERSION_LATEST); | |
JS_SetErrorReporter(cx, reportError); | |
/* Create the global object. */ | |
global = JS_NewObject(cx, &global_class, NULL, NULL); | |
if (global == NULL) | |
return 1; | |
/* 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. */ | |
JS_DefineFunction( cx , global , "print" , &native_print , 1 , 0 ); | |
JSObject * proto = JS_InitClass( cx , global , 0 , &silly_class , 0 , 0 , 0 , silly_functions , 0 , 0 ); | |
assert( proto ); | |
assert( JS_DefineFunctions( cx , proto , silly_functions ) ); | |
const char * code = | |
"print( 'A:' + new Silly() );" | |
"print( 'B:' + Silly.prototype );" | |
"if ( ! Silly ) throw 'no Silly!';" | |
"if ( (new Silly()).x != 3 ) throw 'Silly.x is bad'; " | |
"print( 'y:' + (new Silly()).y );" | |
"if ( (new Silly()).y() != 4 ) throw 'silly.y() is bad';" | |
"if ( ! Silly.prototype ) throw 'no Silly prototype!';" | |
"x = 5 + 2 + (new Silly()).x;"; | |
jsval ret; | |
JS_EvaluateScript( cx , global, code , strlen( code ) , "anon" , 0 , &ret ); | |
jsval x; | |
JS_GetProperty( cx , global , "x" , &x ); | |
double d; | |
JS_ValueToNumber( cx , x , &d ); | |
printf( "output: %f\n" , d ); | |
/* Cleanup. */ | |
JS_DestroyContext(cx); | |
JS_DestroyRuntime(rt); | |
JS_ShutDown(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, try to change line 112:
JSObject * proto = JS_InitClass( cx , global , 0 , &silly_class , silly_constructor , 0 , 0 , silly_functions , 0 , 0 );