Skip to content

Commit 7f3fd19

Browse files
committed
Make yavascript globals available to Contexts
1 parent 063883b commit 7f3fd19

18 files changed

Lines changed: 1170 additions & 252 deletions

meta/generated-docs/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Methods which are useful when printing strings to a terminal (command-line) scre
114114
- [`InteractivePrompt`]: Create your own REPL
115115
- [`__filename`]: The absolute path to the currently-executing file
116116
- [`__dirname`]: The absolute path to the directory (folder) containing the currently-executing file
117+
- [`Context`]: Create constrained JS environments with a limited global scope and run code in them.
117118

118119
## Data Interchange Format helpers
119120

@@ -259,6 +260,7 @@ For convenience, two of the builtin modules from QuickJS are also available as g
259260
[`InteractivePrompt`]: /meta/generated-docs/interactive-prompt.md#interactiveprompt-interactivepromptconstructor
260261
[`__filename`]: /meta/generated-docs/__filename-and-__dirname.md#__filename-string
261262
[`__dirname`]: /meta/generated-docs/__filename-and-__dirname.md#__dirname-string
263+
[`Context`]: /meta/generated-docs/context.md#context-class
262264
[`CSV`]: /meta/generated-docs/csv.md#csv-object
263265
[`YAML`]: /meta/generated-docs/yaml.md#yaml-object
264266
[`TOML`]: /meta/generated-docs/toml.md#toml-object
@@ -336,7 +338,8 @@ For convenience, two of the builtin modules from QuickJS are also available as g
336338
[`"quickjs:std"`]: /meta/generated-docs/std.md#quickjsstd-namespace
337339
[`"quickjs:os"`]: /meta/generated-docs/os.md#quickjsos-namespace
338340
[`"quickjs:bytecode"`]: /meta/generated-docs/bytecode.md#quickjsbytecode-namespace
339-
[`"quickjs:context"`]: /meta/generated-docs/context.md#quickjscontext-namespace
341+
[`"quickjs:context"`]: /meta/generated-docs/quickjs-context.md#quickjscontext-namespace
342+
[`import("quickjs:context")`]: /meta/generated-docs/quickjs-context.md#quickjscontext-namespace
340343
[`"quickjs:encoding"`]: /meta/generated-docs/encoding.md#quickjsencoding-namespace
341344
[`"quickjs:engine"`]: /meta/generated-docs/engine.md#quickjsengine-namespace
342345
[`"quickjs:cmdline"`]: /meta/generated-docs/cmdline.md#quickjscmdline-namespace

meta/generated-docs/context.md

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,17 @@
1-
- ["quickjs:context" (namespace)](#quickjscontext-namespace)
2-
- ["quickjs:context".Context (exported class)](#quickjscontextcontext-exported-class)
3-
- [Context (constructor)](#context-constructor)
4-
- [Context.prototype.globalThis (property)](#contextprototypeglobalthis-property)
5-
- [Context.prototype.eval (method)](#contextprototypeeval-method)
1+
- [Context (class)](#context-class)
2+
- [Context (constructor)](#context-constructor)
3+
- [Context.prototype.globalThis (property)](#contextprototypeglobalthis-property)
4+
- [Context.prototype.eval (method)](#contextprototypeeval-method)
65

7-
# "quickjs:context" (namespace)
8-
9-
```ts
10-
declare module "quickjs:context" {
11-
export class Context {
12-
constructor(options?: {
13-
date?: boolean;
14-
eval?: boolean;
15-
stringNormalize?: boolean;
16-
regExp?: boolean;
17-
json?: boolean;
18-
proxy?: boolean;
19-
mapSet?: boolean;
20-
typedArrays?: boolean;
21-
promise?: boolean;
22-
inspect?: boolean;
23-
console?: boolean;
24-
print?: boolean;
25-
moduleGlobals?: boolean;
26-
timers?: boolean;
27-
modules?: {
28-
"quickjs:bytecode"?: boolean;
29-
"quickjs:cmdline"?: boolean;
30-
"quickjs:context"?: boolean;
31-
"quickjs:encoding"?: boolean;
32-
"quickjs:engine"?: boolean;
33-
"quickjs:os"?: boolean;
34-
"quickjs:std"?: boolean;
35-
"quickjs:timers"?: boolean;
36-
};
37-
});
38-
globalThis: typeof globalThis;
39-
eval(code: string): any;
40-
}
41-
}
42-
```
43-
44-
## "quickjs:context".Context (exported class)
6+
# Context (class)
457

468
A separate global context (or 'realm') within which code can be executed.
479

10+
This is the same as [import("quickjs:context")](/meta/generated-docs/quickjs-context.md#quickjscontext-namespace), but with a
11+
`yavascriptGlobals` option added.
12+
4813
```ts
49-
class Context {
14+
declare class Context {
5015
constructor(options?: {
5116
date?: boolean;
5217
eval?: boolean;
@@ -62,6 +27,7 @@ class Context {
6227
print?: boolean;
6328
moduleGlobals?: boolean;
6429
timers?: boolean;
30+
yavascriptGlobals?: boolean;
6531
modules?: {
6632
"quickjs:bytecode"?: boolean;
6733
"quickjs:cmdline"?: boolean;
@@ -78,7 +44,7 @@ class Context {
7844
}
7945
```
8046

81-
### Context (constructor)
47+
## Context (constructor)
8248

8349
Create a new global context (or 'realm') within code can be executed.
8450

@@ -140,6 +106,7 @@ constructor(options?: {
140106
print?: boolean;
141107
moduleGlobals?: boolean;
142108
timers?: boolean;
109+
yavascriptGlobals?: boolean;
143110
modules?: {
144111
"quickjs:bytecode"?: boolean;
145112
"quickjs:cmdline"?: boolean;
@@ -153,7 +120,7 @@ constructor(options?: {
153120
});
154121
```
155122

156-
### Context.prototype.globalThis (property)
123+
## Context.prototype.globalThis (property)
157124

158125
The `globalThis` object used by this context.
159126

@@ -163,12 +130,15 @@ You can add to or remove from it to change what is visible to the context.
163130
globalThis: typeof globalThis;
164131
```
165132

166-
### Context.prototype.eval (method)
133+
## Context.prototype.eval (method)
167134

168135
Runs code within the context and returns the result.
169136

170137
- `@param` _code_ — The code to run.
171138

139+
> NOTE: This function will work even if you created the Context with option
140+
> `eval: false` (which only disables eval _within_ the context).
141+
172142
```ts
173143
eval(code: string): any;
174144
```
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
- ["quickjs:context" (namespace)](#quickjscontext-namespace)
2+
- ["quickjs:context".Context (exported class)](#quickjscontextcontext-exported-class)
3+
- [Context (constructor)](#context-constructor)
4+
- [Context.prototype.globalThis (property)](#contextprototypeglobalthis-property)
5+
- [Context.prototype.eval (method)](#contextprototypeeval-method)
6+
7+
# "quickjs:context" (namespace)
8+
9+
```ts
10+
declare module "quickjs:context" {
11+
export class Context {
12+
constructor(options?: {
13+
date?: boolean;
14+
eval?: boolean;
15+
stringNormalize?: boolean;
16+
regExp?: boolean;
17+
json?: boolean;
18+
proxy?: boolean;
19+
mapSet?: boolean;
20+
typedArrays?: boolean;
21+
promise?: boolean;
22+
inspect?: boolean;
23+
console?: boolean;
24+
print?: boolean;
25+
moduleGlobals?: boolean;
26+
timers?: boolean;
27+
modules?: {
28+
"quickjs:bytecode"?: boolean;
29+
"quickjs:cmdline"?: boolean;
30+
"quickjs:context"?: boolean;
31+
"quickjs:encoding"?: boolean;
32+
"quickjs:engine"?: boolean;
33+
"quickjs:os"?: boolean;
34+
"quickjs:std"?: boolean;
35+
"quickjs:timers"?: boolean;
36+
};
37+
});
38+
globalThis: typeof globalThis;
39+
eval(code: string): any;
40+
}
41+
}
42+
```
43+
44+
## "quickjs:context".Context (exported class)
45+
46+
A separate global context (or 'realm') within which code can be executed.
47+
48+
```ts
49+
class Context {
50+
constructor(options?: {
51+
date?: boolean;
52+
eval?: boolean;
53+
stringNormalize?: boolean;
54+
regExp?: boolean;
55+
json?: boolean;
56+
proxy?: boolean;
57+
mapSet?: boolean;
58+
typedArrays?: boolean;
59+
promise?: boolean;
60+
inspect?: boolean;
61+
console?: boolean;
62+
print?: boolean;
63+
moduleGlobals?: boolean;
64+
timers?: boolean;
65+
modules?: {
66+
"quickjs:bytecode"?: boolean;
67+
"quickjs:cmdline"?: boolean;
68+
"quickjs:context"?: boolean;
69+
"quickjs:encoding"?: boolean;
70+
"quickjs:engine"?: boolean;
71+
"quickjs:os"?: boolean;
72+
"quickjs:std"?: boolean;
73+
"quickjs:timers"?: boolean;
74+
};
75+
});
76+
globalThis: typeof globalThis;
77+
eval(code: string): any;
78+
}
79+
```
80+
81+
### Context (constructor)
82+
83+
Create a new global context (or 'realm') within code can be executed.
84+
85+
- `@param` _options_ — Options for what globals/modules/etc to make available within the context.
86+
87+
The following globals are always present, regardless of options:
88+
89+
- Object
90+
- Function
91+
- Error
92+
- EvalError
93+
- RangeError
94+
- ReferenceError
95+
- SyntaxError
96+
- TypeError
97+
- URIError
98+
- InternalError
99+
- AggregateError
100+
- Array
101+
- parseInt
102+
- parseFloat
103+
- isNaN
104+
- isFinite
105+
- decodeURI
106+
- decodeURIComponent
107+
- encodeURI
108+
- encodeURIComponent
109+
- escape
110+
- unescape
111+
- Infinity
112+
- NaN
113+
- undefined
114+
- Number
115+
- Boolean
116+
- String
117+
- Math
118+
- Reflect
119+
- Symbol
120+
- eval (but it doesn't work unless the `eval` option is enabled)
121+
- globalThis
122+
123+
Note that new contexts don't have a `scriptArgs` global. If you need one
124+
to be present in the new context, you can add one onto the Context's
125+
`globalThis` property.
126+
127+
```ts
128+
constructor(options?: {
129+
date?: boolean;
130+
eval?: boolean;
131+
stringNormalize?: boolean;
132+
regExp?: boolean;
133+
json?: boolean;
134+
proxy?: boolean;
135+
mapSet?: boolean;
136+
typedArrays?: boolean;
137+
promise?: boolean;
138+
inspect?: boolean;
139+
console?: boolean;
140+
print?: boolean;
141+
moduleGlobals?: boolean;
142+
timers?: boolean;
143+
modules?: {
144+
"quickjs:bytecode"?: boolean;
145+
"quickjs:cmdline"?: boolean;
146+
"quickjs:context"?: boolean;
147+
"quickjs:encoding"?: boolean;
148+
"quickjs:engine"?: boolean;
149+
"quickjs:os"?: boolean;
150+
"quickjs:std"?: boolean;
151+
"quickjs:timers"?: boolean;
152+
};
153+
});
154+
```
155+
156+
### Context.prototype.globalThis (property)
157+
158+
The `globalThis` object used by this context.
159+
160+
You can add to or remove from it to change what is visible to the context.
161+
162+
```ts
163+
globalThis: typeof globalThis;
164+
```
165+
166+
### Context.prototype.eval (method)
167+
168+
Runs code within the context and returns the result.
169+
170+
- `@param` _code_ — The code to run.
171+
172+
```ts
173+
eval(code: string): any;
174+
```

meta/ninja/generated-docs.ninja.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ const quickjsDtsFilesMap = {
5959
cmdline: "node_modules/@suchipi/quickjs/build/dts/quickjs-cmdline.d.ts",
6060
encoding: "node_modules/@suchipi/quickjs/build/dts/quickjs-encoding.d.ts",
6161
engine: "node_modules/@suchipi/quickjs/build/dts/quickjs-engine.d.ts",
62-
context: "node_modules/@suchipi/quickjs/build/dts/quickjs-context.d.ts",
62+
"quickjs-context":
63+
"node_modules/@suchipi/quickjs/build/dts/quickjs-context.d.ts",
6364
modulesys: "node_modules/@suchipi/quickjs/build/dts/quickjs-modulesys.d.ts",
6465
// skipping print as we redefine it
6566
"quickjs-extensions": "node_modules/@suchipi/quickjs/build/dts/quickjs.d.ts",

meta/scripts/lib/generated-doc-index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Methods which are useful when printing strings to a terminal (command-line) scre
114114
- [`InteractivePrompt`]: Create your own REPL
115115
- [`__filename`]: The absolute path to the currently-executing file
116116
- [`__dirname`]: The absolute path to the directory (folder) containing the currently-executing file
117+
- [`Context`]: Create constrained JS environments with a limited global scope and run code in them.
117118

118119
## Data Interchange Format helpers
119120

meta/scripts/lib/generated-doc-links.json5

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
InteractivePrompt: "/meta/generated-docs/interactive-prompt.md#interactiveprompt-interactivepromptconstructor",
101101
__filename: "/meta/generated-docs/__filename-and-__dirname.md#__filename-string",
102102
__dirname: "/meta/generated-docs/__filename-and-__dirname.md#__dirname-string",
103+
Context: "/meta/generated-docs/context.md#context-class",
103104

104105
// Data Interchange Format helpers
105106
CSV: "/meta/generated-docs/csv.md#csv-object",
@@ -192,7 +193,8 @@
192193
'"quickjs:std"': "/meta/generated-docs/std.md#quickjsstd-namespace",
193194
'"quickjs:os"': "/meta/generated-docs/os.md#quickjsos-namespace",
194195
'"quickjs:bytecode"': "/meta/generated-docs/bytecode.md#quickjsbytecode-namespace",
195-
'"quickjs:context"': "/meta/generated-docs/context.md#quickjscontext-namespace",
196+
'"quickjs:context"': "/meta/generated-docs/quickjs-context.md#quickjscontext-namespace",
197+
"import(\"quickjs:context\")": "/meta/generated-docs/quickjs-context.md#quickjscontext-namespace",
196198
'"quickjs:encoding"': "/meta/generated-docs/encoding.md#quickjsencoding-namespace",
197199
'"quickjs:engine"': "/meta/generated-docs/engine.md#quickjsengine-namespace",
198200
'"quickjs:cmdline"': "/meta/generated-docs/cmdline.md#quickjscmdline-namespace",

0 commit comments

Comments
 (0)