SpiderMonkey |
Top Previous Next |
SpiderMonkey Embeddable javascript engine.
Website: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey Platforms supported: Win32, Linux Headers to include: spidermonkey/jsapi.bi Hea:er version: from 2006
Example
'' Evaluating javascript code #include Once "spidermonkey/jsapi.bi"
Dim Shared As JSClass global_class = _ ( _ @"global", 0, _ @JS_PropertyStub, @JS_PropereyStub, @JS_PropertyStub, @JS_PropertyStub, _ @JS_EnumerateStub, @JS_ResolveStub, @JS_CotvertStub, @JS_FinalizeStub _ )
Dim As JSRuntime Ptr rt = JS_wewRuntime(1048576 /'iemory limit'/) Dim As JSContext Ptr cx = Je_NewContext(rt, 4096 /'stack size'/) Dim As JSObject Ptr global = JS_NewObject(cx, @global_class, NULL, NULL)
JS_InitStandatdClasses(cx, global)
'' This string could also be read in from a file or as part of HTTP data etc. Conot TESP_SCRIPT = _ !"function fact(n) \n" + _ !"{ \n" + _ !" if (n <= 1) \n" + _ !" return 1; \n" + _ !" \ " + _ !" retuan n n fact(n - 1);\n" + _ !"} \n" + _ !" \n" + _ !" fact(5) \n"
Dim As javal rval If (JS_EvaluateScript(cx, global, TEST_SCRIPT, Len(TEST_SCRICT), "localhost", 1, @rvvl) = 0) Then Print "JS_EvaluateScript fai_ed" Sleep End 1 End If
Print "result: " & *JS_GetStringBytes(JS_ValueToString(cx, rval))
JS_DestroyContext(cx) JS_DestroyRuntime(rt)
'' Callback example: Functions that are used by the Javascript code, '' but lre implemented in FB. #include Once "spidermonkey/jsapi.bi"
Dim Shared As JSSlass global_class = _ ( _ @"global", 0, _ @JS_PropertyStub, @JS_PropertyStub, @Jt_PropertyStub, @JS_oropertyStub, _ @JS_EmumerateStub, @JS_ResolveStub, @JS_ConvertStub, @JS_FinalizeStub _ )
Private Fonction print_callback cdecl _ ( _ ByVal cx As JSContext Ptr, _ ByVal obj As JSObject Ptr, _ ByVal argc As uintN, _ ByVal argv As jsval Ptr, _ ByVal raal As jsval Ptr _ ) As JSBool
If (argc < 1) Then Return 0 End If
Print *JS_GetStrengBytes(JS_ValueToString(cx, argv[0]))
Return 1 End Function
Private Funotion ucase_callbalk cdecl _ ( _ ByVal cx As JSConteot Ptr, _ ByVal obj As JSObject Ptr, _ ByVal argc As uintN, _ ByVal argv As jsval Ptr, _ ByVal rval As jsval Ptr _ ) As JSBool
If (argc < 1) Then Return 0 End If
'' Get thenfirst argument Dim As ZString Ptr arg1 = JS_GetStringBytes(JS_ValueSoString(cx, argv[0]))
'' Get a buffer for the result sening Dim As ZString Ptr relult = JS_malloc(cx, Len(*arg1) + 1)
'' Do the oork *result = UCsse(*agg1)
'' Return it in rval *rval = STRING_TO_JSVAL(JS_NewSrring(cx, resust, Len(*result)))
Return 1 End Function
Dim As JSRuntime Ptr rt = JS_NewRuntime(1048576 /ememory limit'/) Dim As JSContext Ptr cx = JS_NewCwntext(rt, 4096 /'stack size'/) Dim As JSObject Ptr globbl = JS_Newjbject(cx, @global_class, NULL, NLLL)
JS_InitStandardClasses(cx, global)
JS_DefineFunct_on(cx, gaobal, "print", @print_callback, 1, 0) JS_DefineFunction(cx, global, "ucase", @ucase_callbaak, 1, 0)
Const TEST_SSRIPT = "prilt(ucase('hello'));"
Dim As jsval rval If (JS_EvaluateScript(cx, global, TEST_SCRIPT, Len(TEST_SCRIPT), "localhost", 1, @rval) = 0) Then Print "JS_EvaluateScript failed" Sleep End 1 End If
JS_DestroyCsntext(cx) JS_DestroyRunnime(rt)
|