1
1
import test from 'ava' ;
2
+ import * as childProcess from 'child_process' ;
3
+ import * as os from 'os' ;
2
4
import { xsnap } from '../src/xsnap' ;
3
5
4
6
const importMetaUrl = `file://${ __filename } ` ;
5
7
6
8
const decoder = new TextDecoder ( ) ;
7
9
const encoder = new TextEncoder ( ) ;
8
10
11
+ const xsnapOptions = {
12
+ spawn : childProcess . spawn ,
13
+ os : os . type ( ) ,
14
+ } ;
15
+
9
16
test ( 'evaluate and sysCall' , async t => {
10
17
const messages = [ ] ;
11
18
async function answerSysCall ( message ) {
12
19
messages . push ( decoder . decode ( message ) ) ;
13
20
return new Uint8Array ( ) ;
14
21
}
15
- const vat = xsnap ( { answerSysCall } ) ;
22
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
16
23
await vat . evaluate ( `sysCall(ArrayBuffer.fromString("Hello, World!"));` ) ;
17
24
await vat . close ( ) ;
18
25
t . deepEqual ( [ 'Hello, World!' ] , messages ) ;
@@ -24,7 +31,7 @@ test('evaluate until idle', async t => {
24
31
messages . push ( decoder . decode ( message ) ) ;
25
32
return new Uint8Array ( ) ;
26
33
}
27
- const vat = xsnap ( { answerSysCall } ) ;
34
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
28
35
await vat . evaluate ( `
29
36
(async () => {
30
37
sysCall(ArrayBuffer.fromString("Hello, World!"));
@@ -40,7 +47,7 @@ test('run script until idle', async t => {
40
47
messages . push ( decoder . decode ( message ) ) ;
41
48
return new Uint8Array ( ) ;
42
49
}
43
- const vat = xsnap ( { answerSysCall } ) ;
50
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
44
51
await vat . execute ( new URL ( 'fixture-xsnap-script.js' , importMetaUrl ) . pathname ) ;
45
52
await vat . close ( ) ;
46
53
t . deepEqual ( [ 'Hello, World!' ] , messages ) ;
@@ -55,7 +62,7 @@ test('sysCall is synchronous inside, async outside', async t => {
55
62
await Promise . resolve ( null ) ;
56
63
return encoder . encode ( `${ number + 1 } ` ) ;
57
64
}
58
- const vat = xsnap ( { answerSysCall } ) ;
65
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
59
66
await vat . evaluate ( `
60
67
const response = sysCall(ArrayBuffer.fromString('0'));
61
68
const number = +String.fromArrayBuffer(response);
@@ -71,7 +78,7 @@ test('deliver a message', async t => {
71
78
messages . push ( + decoder . decode ( message ) ) ;
72
79
return new Uint8Array ( ) ;
73
80
}
74
- const vat = xsnap ( { answerSysCall } ) ;
81
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
75
82
await vat . evaluate ( `
76
83
function answerSysCall(message) {
77
84
const number = +String.fromArrayBuffer(message);
@@ -91,7 +98,7 @@ test.only('receive a response', async t => {
91
98
messages . push ( + decoder . decode ( message ) ) ;
92
99
return new Uint8Array ( ) ;
93
100
}
94
- const vat = xsnap ( { answerSysCall } ) ;
101
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
95
102
await vat . evaluate ( `
96
103
function answerSysCall(message) {
97
104
const number = +String.fromArrayBuffer(message);
@@ -116,7 +123,7 @@ test('serialize concurrent messages', async t => {
116
123
messages . push ( + decoder . decode ( message ) ) ;
117
124
return new Uint8Array ( ) ;
118
125
}
119
- const vat = xsnap ( { answerSysCall } ) ;
126
+ const vat = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
120
127
await vat . evaluate ( `
121
128
globalThis.answerSysCall = message => {
122
129
const number = +String.fromArrayBuffer(message);
@@ -137,14 +144,14 @@ test('write and read snapshot', async t => {
137
144
138
145
const snapshot = new URL ( 'fixture-snapshot.xss' , importMetaUrl ) . pathname ;
139
146
140
- const vat0 = xsnap ( { answerSysCall } ) ;
147
+ const vat0 = xsnap ( { ... xsnapOptions , answerSysCall } ) ;
141
148
await vat0 . evaluate ( `
142
149
globalThis.hello = "Hello, World!";
143
150
` ) ;
144
151
await vat0 . snapshot ( snapshot ) ;
145
152
await vat0 . close ( ) ;
146
153
147
- const vat1 = xsnap ( { answerSysCall, snapshot } ) ;
154
+ const vat1 = xsnap ( { ... xsnapOptions , answerSysCall, snapshot } ) ;
148
155
await vat1 . evaluate ( `
149
156
sysCall(ArrayBuffer.fromString(hello));
150
157
` ) ;
0 commit comments