|
| 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 | +``` |
0 commit comments