-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.ts
78 lines (62 loc) · 1.75 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
Isolate,
StdDispatcher,
StdLoader,
getDispatcherAccessors
} from "./plugin/mod.ts";
import { CustomDispatcher } from "./test_dispatcher/mod.ts";
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
const source = `
const data = new Uint8Array([116, 101, 115, 116]);
Deno.core.print(\`Some test TEXT\n\`);
async function callOp(opId) {
Deno.core.print(\`GUEST RUNTIME CALLING OP \${opId} \n\`);
const response = Deno.core.dispatch(opId, data);
Deno.core.print(\`GUEST RUNTIME RECIEVED RESPONSE \${response} \n\`);
}
function main() {
let ops = Deno.core.ops();
let testOpId = ops.testOp;
let testOpJsId = ops.testOpJs;
callOp(testOpId);
callOp(testOpJsId);
}
main();
`;
const loader = new StdLoader(
(specifier, referrer, isRoot) => {
console.log(`RESOLVE REQUEST ${specifier} ${referrer} ${isRoot}`);
return "file:///testmod.js";
},
moduleSpecifier => {
console.log(`LOAD REQUEST ${moduleSpecifier}`);
return {
module_name: moduleSpecifier,
code: source
};
}
);
const isolate = new Isolate(loader, {
will_snapshot: true
});
const dispatcher = new StdDispatcher();
dispatcher.ondispatch = (
data: Uint8Array,
zero_copy?: Uint8Array
): Uint8Array => {
console.log(`HOST RUNTIME RECIEVED DISPATCH ${textDecoder.decode(data)}`);
const response = textEncoder.encode("Hello World!");
console.log(`HOST RUNTIME SENDING RESPONSE ${response}`);
return response;
};
const customDispatcher = new CustomDispatcher();
isolate.registerOp("testOp", customDispatcher);
isolate.registerOp("testOpJs", dispatcher);
async function main() {
console.log("PRE EXECUTE");
await isolate.execute(source);
Deno.exit();
}
main();
console.log(getDispatcherAccessors());