@ai-ecoverse/slicc-browsers 1.51.0 → 1.53.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/engine.mjs CHANGED
@@ -1,7 +1,19 @@
1
- import bytes from './engine/engine-wasm.mjs';
1
+ import {
2
+ every,
3
+ exec,
4
+ includes,
5
+ method,
6
+ startsWith,
7
+ typedLength,
8
+ typedSet,
9
+ wellFormed,
10
+ } from './engine/builtins.mjs';
11
+ import { createInstance, prepare } from './engine/runtime.mjs';
2
12
 
3
13
  const encoder = new TextEncoder();
4
14
  const decoder = new TextDecoder('utf-8', { fatal: true });
15
+ const encodeText = method(encoder.encode);
16
+ const decodeText = method(decoder.decode);
5
17
  const roles = [
6
18
  'seven',
7
19
  'slicc',
@@ -12,7 +24,6 @@ const roles = [
12
24
  'unparseable',
13
25
  'ordinary',
14
26
  ];
15
- let compiled;
16
27
 
17
28
  const captureDecision = (v) => v === 'allow' || v === 'blank';
18
29
  const record = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
@@ -23,8 +34,9 @@ function valid(request, result) {
23
34
  case 'frameClass':
24
35
  case 'urlClass':
25
36
  return (
37
+ (request.op === 'urlClass' || typeof result?.sealed === 'boolean') &&
26
38
  typeof result?.protected === 'boolean' &&
27
- roles.includes(result.role) &&
39
+ includes(roles, result.role) &&
28
40
  (result.origin === null || typeof result.origin === 'string')
29
41
  );
30
42
  case 'info':
@@ -32,18 +44,20 @@ function valid(request, result) {
32
44
  result?.abi === 1 &&
33
45
  Number.isInteger(result.catalogSchema) &&
34
46
  typeof result.digest === 'string' &&
35
- /^[0-9a-f]{16}$/.test(result.digest)
47
+ exec(/^[0-9a-f]{16}$/, result.digest) !== null
36
48
  );
37
49
  case 'page':
38
50
  return (
39
51
  typeof result?.mixed === 'boolean' &&
40
52
  record(result.frames) &&
41
53
  Object.keys(result.frames).length === request.frames.length &&
42
- request.frames.every((f) => Object.hasOwn(result.frames, f.id)) &&
43
- Object.values(result.frames).every(
54
+ every(request.frames, (f) => Object.hasOwn(result.frames, f.id)) &&
55
+ every(
56
+ Object.values(result.frames),
44
57
  (v) =>
45
58
  typeof v?.protected === 'boolean' &&
46
- roles.includes(v.role) &&
59
+ typeof v.sealed === 'boolean' &&
60
+ includes(roles, v.role) &&
47
61
  captureDecision(v.capture)
48
62
  )
49
63
  );
@@ -60,7 +74,7 @@ function valid(request, result) {
60
74
  typeof result.reason === 'string'))
61
75
  );
62
76
  case 'refusal':
63
- return typeof result === 'string' && result.startsWith('slicc: refused: ');
77
+ return typeof result === 'string' && startsWith(result, 'slicc: refused: ');
64
78
  case 'har':
65
79
  return result === 'include' || result === 'exclude';
66
80
  case 'storageKey':
@@ -71,14 +85,15 @@ function valid(request, result) {
71
85
  }
72
86
 
73
87
  function exchange(wasm, method, input) {
74
- const ptr = wasm.engine_alloc(input.length) >>> 0;
75
- new Uint8Array(wasm.memory.buffer, ptr, input.length).set(input);
76
- const packed = wasm[method](ptr, input.length);
88
+ const size = typedLength(input);
89
+ const ptr = wasm.engine_alloc(size) >>> 0;
90
+ typedSet(new Uint8Array(wasm.memory.buffer, ptr, size), input);
91
+ const packed = wasm[method](ptr, size);
77
92
  const start = Number(BigInt.asUintN(64, packed) >> 32n);
78
93
  const length = Number(packed & 0xffffffffn);
79
- const answer = JSON.parse(decoder.decode(new Uint8Array(wasm.memory.buffer, start, length)));
94
+ const answer = JSON.parse(decodeText(decoder, new Uint8Array(wasm.memory.buffer, start, length)));
80
95
  wasm.engine_free(start, length);
81
- wasm.engine_free(ptr, input.length);
96
+ wasm.engine_free(ptr, size);
82
97
  if (typeof answer?.error === 'string') throw new Error(answer.error);
83
98
  if (!answer || Object.keys(answer).length !== 1 || !Object.hasOwn(answer, 'ok'))
84
99
  throw new Error('invalid engine response');
@@ -86,31 +101,16 @@ function exchange(wasm, method, input) {
86
101
  }
87
102
 
88
103
  function encode(value) {
89
- return encoder.encode(
90
- JSON.stringify(value, (_, v) => (typeof v === 'string' ? v.toWellFormed() : v))
104
+ return encodeText(
105
+ encoder,
106
+ JSON.stringify(value, (_, v) => (typeof v === 'string' ? wellFormed(v) : v))
91
107
  );
92
108
  }
93
109
 
94
- function preserveFailedURL(request) {
95
- if (
96
- !['target', 'frame', 'targetClass', 'frameClass'].includes(request?.op) ||
97
- typeof request.url !== 'string'
98
- )
99
- return request;
100
- const failed = request.unreachableURL ?? request.unreachableUrl;
101
- if (
102
- typeof failed !== 'string' ||
103
- failed === request.url ||
104
- failed.toWellFormed() !== request.url.toWellFormed()
105
- )
106
- return request;
107
- return { ...request, documentURL: undefined };
108
- }
109
-
110
- function binding(module, config) {
110
+ export function engine(config = {}) {
111
111
  const input = encode(config);
112
112
  function create() {
113
- const wasm = new WebAssembly.Instance(module).exports;
113
+ const wasm = createInstance();
114
114
  if (wasm.engine_abi() !== 1) throw new Error('unsupported engine ABI');
115
115
  if (exchange(wasm, 'engine_init', input) !== true)
116
116
  throw new Error('engine initialization failed');
@@ -120,16 +120,12 @@ function binding(module, config) {
120
120
  function call(request) {
121
121
  try {
122
122
  instance ??= create();
123
- const wire =
124
- request.op === 'batch' && Array.isArray(request.calls)
125
- ? { ...request, calls: request.calls.map(preserveFailedURL) }
126
- : preserveFailedURL(request);
127
- const result = exchange(instance, 'engine_call', encode(wire));
123
+ const result = exchange(instance, 'engine_call', encode(request));
128
124
  const ok =
129
125
  request.op === 'batch'
130
126
  ? Array.isArray(result) &&
131
127
  result.length === request.calls.length &&
132
- result.every((v, i) => valid(request.calls[i], v))
128
+ every(result, (v, i) => valid(request.calls[i], v))
133
129
  : valid(request, result);
134
130
  if (!ok) throw new Error(`invalid engine result for ${request.op}`);
135
131
  return result;
@@ -149,6 +145,7 @@ function binding(module, config) {
149
145
  frame: (facts) => call({ ...facts, op: 'frame' }),
150
146
  targetClass: (facts) => call({ ...facts, op: 'targetClass' }),
151
147
  frameClass: (facts) => call({ ...facts, op: 'frameClass' }),
148
+ sealed: (facts) => call({ ...facts, op: 'sealed' }),
152
149
  kept: (url) => call({ op: 'kept', url }),
153
150
  keptTarget: (facts) => call({ ...facts, op: 'keptTarget' }),
154
151
  unparseable: (url) => call({ op: 'unparseable', url }),
@@ -165,12 +162,7 @@ function binding(module, config) {
165
162
  };
166
163
  }
167
164
 
168
- export function engine(config = {}) {
169
- compiled ??= new WebAssembly.Module(bytes);
170
- return binding(compiled, config);
171
- }
172
-
173
165
  export async function loadEngine(config = {}) {
174
- compiled ??= await WebAssembly.compile(bytes);
175
- return binding(compiled, config);
166
+ await prepare();
167
+ return engine(config);
176
168
  }