2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-04-09 11:54:25 -04:00
|
|
|
// deno-lint-ignore-file no-explicit-any
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import { notImplemented } from "ext:deno_node/_utils.ts";
|
2024-04-09 11:54:25 -04:00
|
|
|
import {
|
|
|
|
op_vm_create_context,
|
|
|
|
op_vm_create_script,
|
|
|
|
op_vm_is_context,
|
|
|
|
op_vm_script_run_in_context,
|
|
|
|
op_vm_script_run_in_this_context,
|
|
|
|
} from "ext:core/ops";
|
2023-04-19 10:26:16 -04:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export class Script {
|
2024-04-09 11:54:25 -04:00
|
|
|
#inner;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
constructor(code: string, _options = {}) {
|
2024-04-09 11:54:25 -04:00
|
|
|
this.#inner = op_vm_create_script(code);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runInThisContext(_options: any) {
|
2024-04-09 11:54:25 -04:00
|
|
|
return op_vm_script_run_in_this_context(this.#inner);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2024-04-09 11:54:25 -04:00
|
|
|
runInContext(contextifiedObject: any, _options: any) {
|
|
|
|
return op_vm_script_run_in_context(this.#inner, contextifiedObject);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-12-11 02:08:45 -05:00
|
|
|
runInNewContext(contextObject: any, options: any) {
|
2024-04-09 11:54:25 -04:00
|
|
|
const context = createContext(contextObject);
|
|
|
|
return this.runInContext(context, options);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
createCachedData() {
|
2024-04-09 11:54:25 -04:00
|
|
|
notImplemented("Script.prototype.createCachedData");
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 11:54:25 -04:00
|
|
|
export function createContext(contextObject: any = {}, _options: any) {
|
|
|
|
if (isContext(contextObject)) {
|
|
|
|
return contextObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
op_vm_create_context(contextObject);
|
|
|
|
return contextObject;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createScript(code: string, options: any) {
|
|
|
|
return new Script(code, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function runInContext(
|
2024-04-09 11:54:25 -04:00
|
|
|
code: string,
|
|
|
|
contextifiedObject: any,
|
2023-02-14 11:38:45 -05:00
|
|
|
_options: any,
|
|
|
|
) {
|
2024-04-09 11:54:25 -04:00
|
|
|
return createScript(code).runInContext(contextifiedObject);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function runInNewContext(
|
2023-12-11 02:08:45 -05:00
|
|
|
code: string,
|
|
|
|
contextObject: any,
|
|
|
|
options: any,
|
2023-02-14 11:38:45 -05:00
|
|
|
) {
|
2023-12-11 02:08:45 -05:00
|
|
|
if (options) {
|
|
|
|
console.warn("vm.runInNewContext options are currently not supported");
|
|
|
|
}
|
2024-04-09 11:54:25 -04:00
|
|
|
return createScript(code).runInNewContext(contextObject);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function runInThisContext(
|
|
|
|
code: string,
|
|
|
|
options: any,
|
|
|
|
) {
|
|
|
|
return createScript(code, options).runInThisContext(options);
|
|
|
|
}
|
|
|
|
|
2024-04-09 11:54:25 -04:00
|
|
|
export function isContext(maybeContext: any) {
|
|
|
|
return op_vm_is_context(maybeContext);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function compileFunction(_code: string, _params: any, _options: any) {
|
|
|
|
notImplemented("compileFunction");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function measureMemory(_options: any) {
|
|
|
|
notImplemented("measureMemory");
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
Script,
|
|
|
|
createContext,
|
|
|
|
createScript,
|
|
|
|
runInContext,
|
|
|
|
runInNewContext,
|
|
|
|
runInThisContext,
|
|
|
|
isContext,
|
|
|
|
compileFunction,
|
|
|
|
measureMemory,
|
|
|
|
};
|