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
|
|
|
|
2023-06-27 02:18:22 -04:00
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file no-explicit-any prefer-primordials
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import { core } from "ext:core/mod.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { notImplemented } from "ext:deno_node/_utils.ts";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
op_vm_run_in_new_context,
|
|
|
|
} = core.ensureFastOps();
|
2023-04-19 10:26:16 -04:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export class Script {
|
|
|
|
code: string;
|
|
|
|
constructor(code: string, _options = {}) {
|
|
|
|
this.code = `${code}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
runInThisContext(_options: any) {
|
2023-04-19 10:26:16 -04:00
|
|
|
const [result, error] = core.evalContext(this.code, "data:");
|
|
|
|
if (error) {
|
|
|
|
throw error.thrown;
|
|
|
|
}
|
|
|
|
return result;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runInContext(_contextifiedObject: any, _options: any) {
|
|
|
|
notImplemented("Script.prototype.runInContext");
|
|
|
|
}
|
|
|
|
|
2023-12-11 02:08:45 -05:00
|
|
|
runInNewContext(contextObject: any, options: any) {
|
|
|
|
if (options) {
|
|
|
|
console.warn(
|
|
|
|
"Script.runInNewContext options are currently not supported",
|
|
|
|
);
|
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_vm_run_in_new_context(this.code, contextObject);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
createCachedData() {
|
|
|
|
notImplemented("Script.prototyp.createCachedData");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createContext(_contextObject: any, _options: any) {
|
|
|
|
notImplemented("createContext");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createScript(code: string, options: any) {
|
|
|
|
return new Script(code, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function runInContext(
|
|
|
|
_code: string,
|
|
|
|
_contextifiedObject: any,
|
|
|
|
_options: any,
|
|
|
|
) {
|
|
|
|
notImplemented("runInContext");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-10 17:37:25 -05:00
|
|
|
return op_vm_run_in_new_context(code, contextObject);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function runInThisContext(
|
|
|
|
code: string,
|
|
|
|
options: any,
|
|
|
|
) {
|
|
|
|
return createScript(code, options).runInThisContext(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isContext(_maybeContext: any) {
|
2023-12-14 05:19:50 -05:00
|
|
|
// TODO(@littledivy): Currently we do not expose contexts so this is always false.
|
|
|
|
return false;
|
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,
|
|
|
|
};
|