Mercurial > spidermonkey-playground
annotate spidermonkey-playground.cpp @ 58:0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 24 Jun 2009 21:15:45 -0700 |
parents | 8750e9ecf3af |
children | 4910bc49a182 |
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 |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
45 #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
|
46 #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
|
47 #include "memory_profiler.h" |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
48 |
26 | 49 // 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
|
50 #define TCB_FILENAME "tcb.js" |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
51 |
26 | 52 // 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
|
53 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
|
54 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
|
55 |
17 | 56 // TODO: Make sure we're rooting objects appropriately here so that |
57 // the interpreter doesn't randomly crash or something. | |
58 | |
0 | 59 /* The class of the global object. */ |
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
|
60 JSClass global_class = { |
0 | 61 "global", JSCLASS_GLOBAL_FLAGS, |
62 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
63 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
64 JSCLASS_NO_OPTIONAL_MEMBERS | |
65 }; | |
66 | |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
67 // This native JS function prints the given string to the console. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
68 |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
69 static JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
70 jsval *rval) |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
71 { |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
72 char *str; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
73 |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
74 if (!JS_ConvertArguments(cx, argc, argv, "s", &str)) |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
75 return JS_FALSE; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
76 |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
77 printf("%s\n", str); |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
78 |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
79 return JS_TRUE; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
80 } |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
81 |
35
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
82 // This native JS function forces garbage collection. |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
83 |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
84 static JSBool forceGC(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
85 jsval *rval) |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
86 { |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
87 JS_GC(cx); |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
88 return JS_TRUE; |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
89 } |
5899faf625d4
Added a forceGC() function to the global TCB object.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
90 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
91 // 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
|
92 // require() function. For more information, see: |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
93 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
94 // https://wiki.mozilla.org/ServerJS/Modules/SecurableModules |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
95 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
96 // 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
|
97 // 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
|
98 // 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
|
99 // |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
100 // 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
|
101 |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
102 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
|
103 jsval *rval) |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
104 { |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
105 char *filename; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
106 JSObject *exports; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
107 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
108 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
|
109 return JS_FALSE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
110 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
111 FILE *f = fopen(filename, "r"); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
112 if (!f) { |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
113 JS_ReportError(cx, "File not found"); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
114 return JS_FALSE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
115 } |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
116 |
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
|
117 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
|
118 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
|
119 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
|
120 |
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
|
121 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
|
122 fread(source, fileSize, 1, f); |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
123 fclose(f); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
124 |
16 | 125 // TODO: Check for return values here. |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
126 JSContext *module_cx = JS_NewContext(JS_GetRuntime(cx), 8192); |
16 | 127 JSObject *module_global = JS_NewObject(module_cx, &global_class, NULL, |
128 NULL); | |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
129 JS_InitStandardClasses(module_cx, module_global); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
130 JS_DefineProperty(module_cx, module_global, "imports", |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
131 OBJECT_TO_JSVAL(exports), NULL, NULL, 0); |
16 | 132 JSObject *module_exports = JS_NewObject(module_cx, NULL, NULL, |
133 module_global); | |
6
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
134 JS_DefineProperty(module_cx, module_global, "exports", |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
135 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
|
136 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
137 jsval module_rval; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
138 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
|
139 strlen(source), filename, 1, &module_rval)) { |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
140 JS_DestroyContext(module_cx); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
141 return JS_FALSE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
142 } |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
143 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
144 *rval = OBJECT_TO_JSVAL(module_exports); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
145 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
146 JS_DestroyContext(module_cx); |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
147 |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
148 return JS_TRUE; |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
149 } |
500e267ed094
Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
150 |
33
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
151 // This native JS function is a wrapper for JS_Enumerate(). |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
152 |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
153 static JSBool enumerate(JSContext *cx, JSObject *obj, uintN argc, |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
154 jsval *argv, jsval *rval) |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
155 { |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
156 JSObject *target; |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
157 |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
158 if (!JS_ConvertArguments(cx, argc, argv, "o", &target)) |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
159 return JS_FALSE; |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
160 |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
161 JSIdArray *ids = JS_Enumerate(cx, target); |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
162 |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
163 if (ids == NULL) |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
164 return JS_FALSE; |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
165 |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
166 JSObject *array = JS_NewArrayObject(cx, ids->length, ids->vector); |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
167 *rval = OBJECT_TO_JSVAL(array); |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
168 return JS_TRUE; |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
169 } |
cb058fac14fa
Added an enumerate() function to the TCB global.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
170 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
171 // This native JS function looks up the property of an object, bypassing |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
172 // security checks and getters/setters. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
173 |
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
|
174 static JSBool lookupProperty(JSContext *cx, JSObject *obj, uintN argc, |
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
|
175 jsval *argv, jsval *rval) |
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
|
176 { |
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
|
177 JSObject *target; |
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
|
178 |
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
|
179 if (argc < 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
|
180 JS_ReportError(cx, "Must provide id to lookup."); |
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
|
181 return JS_FALSE; |
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
|
182 } |
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
|
183 |
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
|
184 if (!JS_ConvertArguments(cx, argc, argv, "o", &target)) |
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
|
185 return JS_FALSE; |
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
|
186 |
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
|
187 return JS_LookupPropertyById(cx, target, argv[1], rval); |
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
|
188 } |
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
|
189 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
190 // This native JS function returns a JS object containing metadata about |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
191 // the given function. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
192 |
15
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
193 static JSBool functionInfo(JSContext *cx, JSObject *obj, uintN argc, |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
194 jsval *argv, jsval *rval) |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
195 { |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
196 JSFunction *func; |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
197 |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
198 if (!JS_ConvertArguments(cx, argc, argv, "f", &func)) |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
199 return JS_FALSE; |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
200 |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
201 JSScript *script = JS_GetFunctionScript(cx, func); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
202 |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
203 if (script == NULL) { |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
204 *rval = JSVAL_NULL; |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
205 return JS_TRUE; |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
206 } |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
207 |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
208 jsval filenameVal = JSVAL_NULL; |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
209 |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
210 const char *filename = JS_GetScriptFilename(cx, script); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
211 if (filename) { |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
212 JSString *filenameStr = JS_NewStringCopyZ(cx, filename); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
213 filenameVal = STRING_TO_JSVAL(filenameStr); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
214 } |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
215 |
16 | 216 // TODO: Check for return values here. |
15
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
217 JSObject *funcInfo = JS_NewObject(cx, NULL, NULL, NULL); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
218 JS_DefineProperty(cx, funcInfo, "filename", filenameVal, |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
219 NULL, NULL, 0); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
220 |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
221 *rval = OBJECT_TO_JSVAL(funcInfo); |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
222 return JS_TRUE; |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
223 } |
1d73446eef62
added functionInfo() native function.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
224 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
225 // This native JS function returns a JS representation of the current |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
226 // state of the stack, starting with the callee's stack frame and going |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
227 // up from there. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
228 |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
229 static JSBool stack(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
230 jsval *rval) |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
231 { |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
232 JSStackFrame *iterator = NULL; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
233 JSStackFrame *frame; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
234 JSObject *prevFrameInfo = NULL; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
235 JSObject *firstFrameInfo = NULL; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
236 bool skippedMyFrame = false; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
237 |
5
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
238 if (obj == NULL) |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
239 // We're being called from native code, don't skip the topmost frame. |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
240 skippedMyFrame = true; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
241 |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
242 while ((frame = JS_FrameIterator(cx, &iterator)) != NULL) { |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
243 if (!skippedMyFrame) { |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
244 skippedMyFrame = true; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
245 continue; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
246 } |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
247 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
248 jsval functionNameVal = JSVAL_NULL; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
249 jsval filenameVal = JSVAL_NULL; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
250 jsval lineNoVal = JSVAL_ZERO; |
12
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
251 jsval functionObjectVal = JSVAL_NULL; |
13
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
252 jsval scopeChainVal = JSVAL_NULL; |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
253 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
254 JSFunction *func = JS_GetFrameFunction(cx, frame); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
255 if (func) { |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
256 JSString *functionName = JS_GetFunctionId(func); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
257 if (functionName) |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
258 functionNameVal = STRING_TO_JSVAL(functionName); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
259 } |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
260 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
261 if (!JS_IsNativeFrame(cx, frame)) { |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
262 JSScript *script = JS_GetFrameScript(cx, frame); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
263 jsbytecode *bytecode = JS_GetFramePC(cx, frame); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
264 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
265 const char *filename = JS_GetScriptFilename(cx, script); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
266 if (filename) { |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
267 JSString *filenameStr = JS_NewStringCopyZ(cx, filename); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
268 filenameVal = STRING_TO_JSVAL(filenameStr); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
269 } |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
270 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
271 uintN lineNo = JS_PCToLineNumber(cx, script, bytecode); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
272 lineNoVal = INT_TO_JSVAL(lineNo); |
12
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
273 |
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
274 JSObject *functionObject = JS_GetFrameFunctionObject(cx, frame); |
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
275 functionObjectVal = OBJECT_TO_JSVAL(functionObject); |
13
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
276 |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
277 JSObject *scopeChain = JS_GetFrameScopeChain(cx, frame); |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
278 scopeChainVal = OBJECT_TO_JSVAL(scopeChain); |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
279 } |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
280 |
16 | 281 // TODO: Check for return values here. |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
282 JSObject *frameInfo = JS_NewObject(cx, NULL, NULL, NULL); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
283 JS_DefineProperty(cx, frameInfo, "filename", filenameVal, |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
284 NULL, NULL, 0); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
285 JS_DefineProperty(cx, frameInfo, "lineNo", lineNoVal, |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
286 NULL, NULL, 0); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
287 JS_DefineProperty(cx, frameInfo, "functionName", functionNameVal, |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
288 NULL, NULL, 0); |
12
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
289 JS_DefineProperty(cx, frameInfo, "functionObject", functionObjectVal, |
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
290 NULL, NULL, 0); |
13
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
291 JS_DefineProperty(cx, frameInfo, "scopeChain", scopeChainVal, |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
292 NULL, NULL, 0); |
12
e14f433f3a58
Function objects are now attached to stack frames returned by stack().
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
293 |
4
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
294 if (prevFrameInfo) |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
295 JS_DefineProperty(cx, prevFrameInfo, "caller", |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
296 OBJECT_TO_JSVAL(frameInfo), NULL, NULL, 0); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
297 else |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
298 firstFrameInfo = frameInfo; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
299 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
300 prevFrameInfo = frameInfo; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
301 } |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
302 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
303 if (firstFrameInfo) |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
304 *rval = OBJECT_TO_JSVAL(firstFrameInfo); |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
305 else |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
306 *rval = JSVAL_NULL; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
307 |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
308 return JS_TRUE; |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
309 } |
71de19be1054
Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
310 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
311 // Our global hook called whenever an exception is thrown saves the current |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
312 // exception and stack information to the 'lastException' and |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
313 // 'lastExceptionTraceback' globals of the TCB, respectively, much like |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
314 // Python's sys.exc_info(). |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
315 |
5
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
316 static JSTrapStatus throwHook(JSContext *cx, JSScript *script, jsbytecode *pc, |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
317 jsval *rval, void *closure) |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
318 { |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
319 JSObject *tcb_global = (JSObject *) closure; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
320 jsval lastExceptionTraceback; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
321 jsval lastException; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
322 |
13
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
323 jsval exception = *rval; |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
324 if (JS_IsExceptionPending(cx)) |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
325 if (!JS_GetPendingException(cx, &exception)) |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
326 printf("Getting exception failed.\n"); |
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
327 |
5
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
328 if (!JS_GetProperty(cx, tcb_global, "lastException", &lastException)) |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
329 printf("Unable to retrieve last exception."); |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
330 |
13
d200a8158bd5
Fixed a bug in the throw hook, added a 'scopeChain' property to stack frames.
Atul Varma <varmaa@toolness.com>
parents:
12
diff
changeset
|
331 if (lastException == exception) |
5
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
332 // The same exception is just propagating through the stack; keep |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
333 // our existing last-exception info. |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
334 return JSTRAP_CONTINUE; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
335 |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
336 if (!stack(cx, NULL, 0, NULL, &lastExceptionTraceback)) { |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
337 printf("Generation of exception info failed."); |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
338 lastExceptionTraceback = JSVAL_NULL; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
339 } |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
340 |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
341 if (!JS_SetProperty(cx, tcb_global, "lastExceptionTraceback", |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
342 &lastExceptionTraceback) || |
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
|
343 !JS_SetProperty(cx, tcb_global, "lastException", &exception)) |
5
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
344 printf("Setting of exception info failed."); |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
345 |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
346 return JSTRAP_CONTINUE; |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
347 } |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
348 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
349 // 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
|
350 // '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
|
351 // not, though, we do a default checkAccess. |
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
352 |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
353 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
|
354 JSAccessMode mode, jsval *vp) |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
355 { |
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
|
356 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
|
357 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
|
358 &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
|
359 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 vp); |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
366 } |
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
|
367 |
23
a94e7cc0727a
Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
368 // 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
|
369 // things too. |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
370 return JS_LookupPropertyById(cx, obj, id, vp); |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
371 } |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
372 |
3 | 373 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
|
374 JS_FS("print", print, 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
|
375 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
|
376 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
|
377 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
|
378 JS_FS("stack", stack, 0, 0, 0), |
f86740dc5fa0
Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
379 JS_FS("require", require, 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
|
380 JS_FS("lookupProperty", lookupProperty, 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
|
381 JS_FS("functionInfo", functionInfo, 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
|
382 JS_FS("enumerate", enumerate, 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
|
383 JS_FS("forceGC", forceGC, 0, 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
|
384 JS_FS("profileMemory", profileMemory, 0, 0, 0), |
41
f495677654fc
renamed Socket to ServerSocket.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
385 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
|
386 JS_FS_END |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
387 }; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
388 |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
389 static JSSecurityCallbacks securityCallbacks = { |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
390 checkAccess, |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
391 NULL, |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
392 NULL |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
393 }; |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
394 |
0 | 395 int main(int argc, const char *argv[]) |
396 { | |
397 /* JS variables. */ | |
398 JSRuntime *rt; | |
399 | |
400 /* Create a JS runtime. */ | |
401 rt = JS_NewRuntime(8L * 1024L * 1024L); | |
402 if (rt == NULL) | |
403 return 1; | |
404 | |
9
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
405 JS_SetRuntimeSecurityCallbacks(rt, &securityCallbacks); |
fcefc3e5a9df
Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents:
8
diff
changeset
|
406 |
0 | 407 /* Create a context. */ |
3 | 408 tcb_cx = JS_NewContext(rt, 8192); |
409 if (tcb_cx == NULL) | |
0 | 410 return 1; |
3 | 411 JS_SetOptions(tcb_cx, JSOPTION_VAROBJFIX); |
412 JS_SetVersion(tcb_cx, JSVERSION_LATEST); | |
0 | 413 |
58
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
414 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
|
415 |
3 | 416 /* Create the TCB's global object. */ |
417 tcb_global = JS_NewObject(tcb_cx, &global_class, NULL, NULL); | |
418 if (tcb_global == NULL) | |
0 | 419 return 1; |
420 | |
58
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
421 JS_AddNamedRoot(tcb_cx, &tcb_global, "TCB Global"); |
51
8750e9ecf3af
Added a getGCRoots() function.
Atul Varma <varmaa@toolness.com>
parents:
50
diff
changeset
|
422 |
3 | 423 /* Populate the tcb_global object with the standard globals, |
0 | 424 like Object and Array. */ |
3 | 425 if (!JS_InitStandardClasses(tcb_cx, tcb_global)) |
0 | 426 return 1; |
427 | |
3 | 428 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
|
429 return 1; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
430 |
16 | 431 // TODO: Check for return values here. |
5
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
432 JS_SetThrowHook(rt, throwHook, tcb_global); |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
433 JS_DefineProperty(tcb_cx, tcb_global, "lastExceptionTraceback", JSVAL_NULL, |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
434 NULL, NULL, 0); |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
435 JS_DefineProperty(tcb_cx, tcb_global, "lastException", JSVAL_NULL, |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
436 NULL, NULL, 0); |
1f38f4f61768
added global lastException and lastExceptionTraceback properties to the TCB global, which are similar to python's exc_info().
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
437 |
0 | 438 /* Your application code here. This may include JSAPI calls |
439 to create your own custom JS objects and run scripts. */ | |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
440 FILE *f = fopen(TCB_FILENAME, "r"); |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
441 if (!f) |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
442 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
|
443 |
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
|
444 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
|
445 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
|
446 fseek(f, 0, SEEK_SET); |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
447 |
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
|
448 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
|
449 fread(source, fileSize, 1, f); |
2
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
450 fclose(f); |
1f3e9c8df4f0
Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
451 |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
452 jsval rval; |
3 | 453 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
|
454 fileSize, TCB_FILENAME, 1, |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
455 &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
|
456 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
|
457 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
|
458 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
|
459 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
|
460 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
|
461 } |
1
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
462 return 1; |
7444443d2646
added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
463 } |
0 | 464 |
465 /* Cleanup. */ | |
58
0b66a265df13
Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
466 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
|
467 JS_EndRequest(tcb_cx); |
3 | 468 JS_DestroyContext(tcb_cx); |
0 | 469 JS_DestroyRuntime(rt); |
470 JS_ShutDown(); | |
471 | |
472 printf("Farewell.\n"); | |
473 return 0; | |
474 } |