Mercurial > spidermonkey-playground
annotate spidermonkey-playground.cpp @ 70:f6e3733eac01
Refactored some code, fixed some TODOs.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 25 Jun 2009 18:11:01 -0700 |
parents | be61430630ab |
children | b87b6ebb6e86 |
rev | line source |
---|---|
18 | 1 /* ***** BEGIN LICENSE BLOCK ***** |
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
3 * | |
4 * The contents of this file are subject to the Mozilla Public License Version | |
5 * 1.1 (the "License"); you may not use this file except in compliance with | |
6 * the License. You may obtain a copy of the License at | |
7 * http://www.mozilla.org/MPL/ | |
8 * | |
9 * Software distributed under the License is distributed on an "AS IS" basis, | |
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
11 * for the specific language governing rights and limitations under the | |
12 * License. | |
13 * | |
14 * The Original Code is Ubiquity. | |
15 * | |
16 * The Initial Developer of the Original Code is Mozilla. | |
17 * Portions created by the Initial Developer are Copyright (C) 2007 | |
18 * the Initial Developer. All Rights Reserved. | |
19 * | |
20 * Contributor(s): | |
21 * Atul Varma <atul@mozilla.com> | |
22 * | |
23 * Alternatively, the contents of this file may be used under the terms of | |
24 * either the GNU General Public License Version 2 or later (the "GPL"), or | |
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
26 * in which case the provisions of the GPL or the LGPL are applicable instead | |
27 * of those above. If you wish to allow use of your version of this file only | |
28 * under the terms of either the GPL or the LGPL, and not to allow others to | |
29 * use your version of this file under the terms of the MPL, indicate your | |
30 * decision by deleting the provisions above and replace them with the notice | |
31 * and other provisions required by the GPL or the LGPL. If you do not delete | |
32 * the provisions above, a recipient may use your version of this file under | |
33 * the terms of any one of the MPL, the GPL or the LGPL. | |
34 * | |
35 * ***** END LICENSE BLOCK ***** */ | |
36 | |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
37 // This file is based on the code in the JSAPI User Guide: |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
38 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
39 // https://developer.mozilla.org/En/SpiderMonkey/JSAPI_User_Guide |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
40 |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
41 #include "string.h" |
0 | 42 #include "jsapi.h" |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
43 #include "jsdbgapi.h" |
0 | 44 |
69
be61430630ab
Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents:
64
diff
changeset
|
45 #include "tcb.h" |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
46 #include "wrapper.h" |
40
f86740dc5fa0
Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
47 #include "server_socket.h" |
50
853f80bd3b4b
Added tentative profileMemory() server, which suspends the current JS runtime, creates a new one and runs a web server in it.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
48 #include "memory_profiler.h" |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
49 |
26 | 50 // The name of our JS script containing our Trusted Code Base (TCB). |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
51 #define TCB_FILENAME "tcb.js" |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
52 |
26 | 53 // Global references to our TCB. |
10
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
54 static JSContext *tcb_cx; |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
55 static JSObject *tcb_global; |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
56 |
69
be61430630ab
Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents:
64
diff
changeset
|
57 // The class of SecurableModules. |
be61430630ab
Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents:
64
diff
changeset
|
58 static JSClass SM_global_class = { |
be61430630ab
Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents:
64
diff
changeset
|
59 "SecurableModuleGlobal", JSCLASS_GLOBAL_FLAGS, |
0 | 60 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, |
61 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
62 JSCLASS_NO_OPTIONAL_MEMBERS | |
63 }; | |
64 | |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
65 // This native JS function is an implementation of the SecurableModule |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
66 // require() function. For more information, see: |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
67 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
68 // https://wiki.mozilla.org/ServerJS/Modules/SecurableModules |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
69 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
70 // The function takes two parameters: a filename to import, and a JS |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
71 // object containing objects to export into the module. The latter is |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
72 // accessible from the loaded module via a global called 'imports'. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
73 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
74 // This function returns the SecurableModule's 'exports' global. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
75 |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
76 static JSBool require(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
77 jsval *rval) |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
78 { |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
79 char *filename; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
80 JSObject *exports; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
81 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
82 if (!JS_ConvertArguments(cx, argc, argv, "so", &filename, &exports)) |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
83 return JS_FALSE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
84 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
85 FILE *f = fopen(filename, "r"); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
86 if (!f) { |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
87 JS_ReportError(cx, "File not found"); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
88 return JS_FALSE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
89 } |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
90 |
39
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
91 fseek(f, 0, SEEK_END); |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
92 long fileSize = ftell(f); |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
93 fseek(f, 0, SEEK_SET); |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
94 |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
95 char source[fileSize]; |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
96 fread(source, fileSize, 1, f); |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
97 fclose(f); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
98 |
16 | 99 // TODO: Check for return values here. |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
100 JSContext *module_cx = JS_NewContext(JS_GetRuntime(cx), 8192); |
64
6a7e87cd1588
Fixed a TODO; executing the whole code now works, though we get GC root leak warnings.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
101 JS_BeginRequest(module_cx); |
69
be61430630ab
Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents:
64
diff
changeset
|
102 JSObject *module_global = JS_NewObject(module_cx, |
be61430630ab
Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents:
64
diff
changeset
|
103 &SM_global_class, NULL, |
16 | 104 NULL); |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
105 JS_InitStandardClasses(module_cx, module_global); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
106 JS_DefineProperty(module_cx, module_global, "imports", |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
107 OBJECT_TO_JSVAL(exports), NULL, NULL, 0); |
16 | 108 JSObject *module_exports = JS_NewObject(module_cx, NULL, NULL, |
109 module_global); | |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
110 JS_DefineProperty(module_cx, module_global, "exports", |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
111 OBJECT_TO_JSVAL(module_exports), NULL, NULL, 0); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
112 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
113 jsval module_rval; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
114 if (!JS_EvaluateScript(module_cx, module_global, source, |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
115 strlen(source), filename, 1, &module_rval)) { |
64
6a7e87cd1588
Fixed a TODO; executing the whole code now works, though we get GC root leak warnings.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
116 JS_EndRequest(module_cx); |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
117 JS_DestroyContext(module_cx); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
118 return JS_FALSE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
119 } |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
120 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
121 *rval = OBJECT_TO_JSVAL(module_exports); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
122 |
64
6a7e87cd1588
Fixed a TODO; executing the whole code now works, though we get GC root leak warnings.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
123 JS_EndRequest(module_cx); |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
124 JS_DestroyContext(module_cx); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
125 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
126 return JS_TRUE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
127 } |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
128 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
129 // Our global checkAccess callback just checks to see if a JS function called |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
130 // 'checkAccess' has been defined in the TCB, and delegates to that if so. If |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
131 // not, though, we do a default checkAccess. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
132 |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
133 static JSBool checkAccess(JSContext *cx, JSObject *obj, jsval id, |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
134 JSAccessMode mode, jsval *vp) |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
135 { |
10
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
136 jsval checkAccess; |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
137 if (tcb_global && JS_GetProperty(tcb_cx, tcb_global, "checkAccess", |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
138 &checkAccess) && |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
139 JSVAL_IS_OBJECT(checkAccess) && |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
140 JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(checkAccess))) { |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
141 jsval argv[2]; |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
142 argv[0] = OBJECT_TO_JSVAL(obj); |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
143 argv[1] = id; |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
144 return JS_CallFunctionValue(tcb_cx, tcb_global, checkAccess, 2, argv, |
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
145 vp); |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
146 } |
10
16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
147 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
148 // TODO: This doesn't work for the 'caller' attribute, and possibly other |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
149 // things too. |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
150 return JS_LookupPropertyById(cx, obj, id, vp); |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
151 } |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
152 |
3 | 153 static JSFunctionSpec tcb_global_functions[] = { |
40
f86740dc5fa0
Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
154 JS_FS("getWrapper", getWrapper, 1, 0, 0), |
f86740dc5fa0
Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
155 JS_FS("unwrap", unwrapObject, 1, 0, 0), |
f86740dc5fa0
Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
156 JS_FS("wrap", wrapObject, 2, 0, 0), |
f86740dc5fa0
Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
157 JS_FS("require", require, 2, 0, 0), |
50
853f80bd3b4b
Added tentative profileMemory() server, which suspends the current JS runtime, creates a new one and runs a web server in it.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
158 JS_FS("profileMemory", profileMemory, 0, 0, 0), |
41
f495677654fc
renamed Socket to ServerSocket.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
159 JS_FS("ServerSocket", createServerSocket, 0, 0, 0), |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
160 JS_FS_END |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
161 }; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
162 |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
163 static JSSecurityCallbacks securityCallbacks = { |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
164 checkAccess, |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
165 NULL, |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
166 NULL |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
167 }; |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
168 |
0 | 169 int main(int argc, const char *argv[]) |
170 { | |
171 /* JS variables. */ | |
172 JSRuntime *rt; | |
173 | |
174 /* Create a JS runtime. */ | |
175 rt = JS_NewRuntime(8L * 1024L * 1024L); | |
176 if (rt == NULL) | |
177 return 1; | |
178 | |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
179 JS_SetRuntimeSecurityCallbacks(rt, &securityCallbacks); |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
180 |
0 | 181 /* Create a context. */ |
3 | 182 tcb_cx = JS_NewContext(rt, 8192); |
183 if (tcb_cx == NULL) | |
0 | 184 return 1; |
3 | 185 JS_SetOptions(tcb_cx, JSOPTION_VAROBJFIX); |
186 JS_SetVersion(tcb_cx, JSVERSION_LATEST); | |
0 | 187 |
58
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
188 JS_BeginRequest(tcb_cx); |
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
189 |
70
f6e3733eac01
Refactored some code, fixed some TODOs.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
190 jsval rval; |
63
4910bc49a182
set GC Zeal to 2 and fixed some bugs exposed by it.
Atul Varma <varmaa@toolness.com>
parents:
58
diff
changeset
|
191 |
70
f6e3733eac01
Refactored some code, fixed some TODOs.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
192 if (!TCB_init(tcb_cx, &rval)) |
0 | 193 return 1; |
70
f6e3733eac01
Refactored some code, fixed some TODOs.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
194 tcb_global = JSVAL_TO_OBJECT(rval); |
0 | 195 |
58
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
196 JS_AddNamedRoot(tcb_cx, &tcb_global, "TCB Global"); |
51
8750e9ecf3af
Added a getGCRoots() function.
Atul Varma <varmaa@toolness.com>
parents:
50
diff
changeset
|
197 |
3 | 198 if (!JS_DefineFunctions(tcb_cx, tcb_global, tcb_global_functions)) |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
199 return 1; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
200 |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
201 FILE *f = fopen(TCB_FILENAME, "r"); |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
202 if (!f) |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
203 return 1; |
39
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
204 |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
205 fseek(f, 0, SEEK_END); |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
206 long fileSize = ftell(f); |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
207 fseek(f, 0, SEEK_SET); |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
208 |
39
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
209 char source[fileSize]; |
067371eb9601
The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
210 fread(source, fileSize, 1, f); |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
211 fclose(f); |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
212 |
3 | 213 if (!JS_EvaluateScript(tcb_cx, tcb_global, source, |
50
853f80bd3b4b
Added tentative profileMemory() server, which suspends the current JS runtime, creates a new one and runs a web server in it.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
214 fileSize, TCB_FILENAME, 1, |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
215 &rval)) { |
8
e14f496e6e08
added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
216 jsval handleError; |
e14f496e6e08
added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
217 if (JS_GetProperty(tcb_cx, tcb_global, "handleError", &handleError) && |
e14f496e6e08
added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
218 JSVAL_IS_OBJECT(handleError) && |
e14f496e6e08
added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
219 JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(handleError))) { |
e14f496e6e08
added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
220 JS_CallFunctionValue(tcb_cx, tcb_global, handleError, 0, NULL, &rval); |
e14f496e6e08
added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
221 } |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
222 return 1; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
223 } |
0 | 224 |
225 /* Cleanup. */ | |
58
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
226 JS_RemoveRoot(tcb_cx, &tcb_global); |
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
227 JS_EndRequest(tcb_cx); |
3 | 228 JS_DestroyContext(tcb_cx); |
0 | 229 JS_DestroyRuntime(rt); |
230 JS_ShutDown(); | |
231 | |
232 printf("Farewell.\n"); | |
233 return 0; | |
234 } |