rusty_racer 0.2.7 → 0.2.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 214a3eb41745b4ed7c93662fa1cc1e1ecd11bc4678d5633eeab9a2075f5b4965
4
- data.tar.gz: 87d473be7421ac6ff8d2c5ef7d2e5039ab75987660c313a37916d3f81def2477
3
+ metadata.gz: f657d7e0bf40a9fcdfc7971e504af9b531d59113c5fff89276e54fba10649c08
4
+ data.tar.gz: 8ba306b2a46d0e0d497a2d7aa599368461ae8da2f5980261512cb2eda48312a3
5
5
  SHA512:
6
- metadata.gz: 8bd2a0dbfd742b01ceb21447a7028401362d3f9cf55ed449e1eae66aae88fa020c9a80edcdd49e606395880d9c54e4a3d3e8af4b20449387ab6246a234fcd6f6
7
- data.tar.gz: 0147def7b482ce78b420bfff341f5575a045ba806c1ea853ad3d24bbe2557735a2e672ed9a1510da8727ce9ae0f01ce7409460d8362b6819f3cc2cc30bffbbd8
6
+ metadata.gz: 496393f627fd387d9c32fd994a857abc24fcd732e8457fc991446f036cce01a26b7e05925b5d55384c59577de2364c8308b142ddd163170fd0534f7df6bb18c2
7
+ data.tar.gz: 72ee79e9052d1fd6c0608dff42b978d5511b782af1ac17f250f8543be7a3dcd3c0f3364e5bc1ae917640d89794411bba0e5f0b72820bd68416d192cce23d9fb8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,92 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.9
4
+
5
+ ### Added
6
+
7
+ - **The host answers `import()`, now or later.** `Isolate#dynamic_import_handler=`
8
+ takes a proc handed a `RustyRacer::DynamicImport` per `import()` — its
9
+ `specifier`, `attributes` (the `with` options' import attributes),
10
+ `referrer`, `host_defined` and `context` — whose promise waits until the host
11
+ settles it, inside the handler or on any later turn: `#resolve(module)` links
12
+ the module (by the block it is given, as `Module#instantiate`'s) and evaluates
13
+ it, `#reject(value)` rejects with a marshalled value, and
14
+ `#reject_with(path, *args)` with what a function of the realm makes — an error
15
+ of the host's own kind, from an `@` path no page reaches. Each answers whether
16
+ the import was still waiting; one whose realm was reset or disposed is not.
17
+ - **Import attributes** reach the resolve block of `Module#instantiate` (a third
18
+ argument, `{"type" => "json"}`), and `Module#requests` lists a module's
19
+ requests with theirs without linking it.
20
+ - **`Module#script_id`**: the id V8's stack frames name the module's code by
21
+ (nil for a synthetic or errored module), so an embedder walking the stack
22
+ (an embedder linking rusty_racer reads `StackFrame::GetScriptId`) can tell
23
+ which module script is running — HTML's active script, say. **`Script#script_id`**
24
+ is a classic script's. Modules or scripts V8's compilation cache shares (one
25
+ source under one filename) share the id.
26
+ - **Synthetic modules:** `Context#synthetic_module(value)`, `(json: text)` —
27
+ ECMA-262's JSON module, parsed by the realm's own `JSON.parse` whatever a page
28
+ made of the global — and `(call: [path, *args])` make a module exporting
29
+ `default` alone.
30
+ - **`host_defined:`** on `Context#compile` and `#compile_module`: a primitive the
31
+ script's origin carries, handed to the `import()`s it makes
32
+ (`DynamicImport#host_defined`) and to its realm's import-meta handler.
33
+ - **`NS.setImportMetaHandler(fn)`**: the realm's `fn(meta, url, hostDefined)`
34
+ fills a module's `import.meta` beyond `url` as it is first read —
35
+ `import.meta.resolve`, for one.
36
+
37
+ ### Changed
38
+
39
+ - `Isolate#dynamic_import_resolver=` is a `dynamic_import_handler` that resolves
40
+ each `import()` at once. A resolver that returns `nil` rejects the import with
41
+ an `Error` naming its specifier, an import made outside any named script hands
42
+ it `""` as its referrer, and `nil` clears it. An `import()` called by a
43
+ function of a realm reset or disposed since rejects, rather than resolving in
44
+ the realm that took its id.
45
+ - A two-parameter lambda as a resolve block is still handed two arguments, one
46
+ with an optional or rest parameter as many as it takes.
47
+ - A resolver that raises rejects the `import()` with an `Error` of its own
48
+ message (it was a generic "import() was not resolved to a module").
49
+ - The attributes a resolve block is handed (and `Module#requests` lists) are
50
+ frozen Hashes.
51
+ - `host_defined:` refuses a Symbol (`ArgumentError`), as it refuses any value
52
+ that is no Integer, Float, String, true or false.
53
+ - A `Context` wrapper of an extra realm reads `disposed?` once that realm is
54
+ disposed through any wrapper — `DynamicImport#context`'s too — and refuses
55
+ operations as a disposed one does.
56
+
57
+ ## 0.2.8
58
+
59
+ ### Added
60
+
61
+ - A **host object** per realm: an object with no prototype that no global
62
+ holds (its global keeps it under a V8 private key), for the host and the
63
+ realm's own bindings to share where a page's script can neither read, call nor
64
+ replace what they put there. Every path the host names starts there when it
65
+ begins with `@`: `Context#attach('@fetch', proc)` / `#attach_many` put a host
66
+ function there (intermediates with no prototype, every property defined, not
67
+ assigned), `#function('@steps.query')` / `#call` / `#call_void` find one
68
+ there, `Module#evaluate(report: '@reportError')` reports to one there, and
69
+ `Isolate.new(host_namespace: '@RustyRacer')` installs rusty_racer's own
70
+ functions (`contextGlobal`, `contextOf`, `drainMicrotasks`, …) there instead
71
+ of on the global. `RustyRacer::HOST_OBJECT`, as an argument (or a host
72
+ function's result) crossing into JS, is the realm's host object — how the
73
+ bindings are handed it, and the realm's host object crosses back into Ruby as
74
+ `HOST_OBJECT`; an embedder linking rusty_racer as a library reaches it
75
+ natively with `rusty_racer::host_object(scope)`, from its realm init hook.
76
+ An `@` path is walked through own properties alone, so nothing a page put on
77
+ a prototype sees or answers the walk. A namespace on the host object has
78
+ `hostObjectOf(id)` besides: another realm's host object, for one realm's
79
+ bindings to reach another's. Made as a realm first uses it, made anew with the
80
+ realm by `reset`.
81
+
82
+ ### Changed
83
+
84
+ - A path beginning with `@` no longer names a property of the global whose name
85
+ begins with `@` (`globalThis["@x"]`): it names the host object's.
86
+ - `Module#evaluate(report:)` looks the reporter up as before — one property,
87
+ dots and all — on the global, or, for an `@` path, as `Context#function` finds
88
+ it on the host object.
89
+
3
90
  ## 0.2.7
4
91
 
5
92
  ### Changed
data/README.md CHANGED
@@ -123,6 +123,62 @@ realm (`frames[0].fn` in an embedder whose realms see each other) holds that
123
123
  realm's function for as long as the handle's own realm lives, as any reference
124
124
  kept there would.
125
125
 
126
+ ### The host object
127
+
128
+ An embedder that runs a page's script beside its own bindings wants the host's
129
+ functions where the bindings can call them and the page can't. Every realm has a
130
+ **host object** for that: an object of the realm with no prototype, which no
131
+ global holds — the global keeps it under a V8 private key, which no script can
132
+ name. A path the host names that begins with `@` starts there instead of at
133
+ `globalThis`:
134
+
135
+ ```ruby
136
+ # rusty_racer's own functions (contextGlobal, contextOf, drainMicrotasks, …)
137
+ # there too, rather than on a global
138
+ iso = RustyRacer::Isolate.new(host_namespace: "@RustyRacer")
139
+ ctx = iso.context
140
+
141
+ # Host functions the page never sees: no global, nothing to read, call or replace.
142
+ ctx.attach("@fetch", ->(url) { fetch(url) })
143
+ ctx.attach_many("@net.open" => open_proc, "@net.close" => close_proc)
144
+
145
+ # The bindings are handed the object as an argument — HOST_OBJECT crosses into
146
+ # JS as the realm's host object — and keep it in a closure or a module binding.
147
+ ctx.call("installBindings", RustyRacer::HOST_OBJECT)
148
+
149
+ # What they put there, the host calls by its path, with no global name.
150
+ query = ctx.function("@steps.query") # host.steps.query, `this` = host.steps
151
+ query.call("#main")
152
+ ctx.call("@steps.flush")
153
+ mod.evaluate(report: "@reportError") # Module#evaluate's reporter, too
154
+ ```
155
+
156
+ An `@` path is walked through **own** properties alone: the intermediates an
157
+ `@` attach makes (`@net` above) have no prototype either, every property it
158
+ makes is *defined*, and a segment an object lacks reads as missing rather than
159
+ off its prototype — so no accessor a page put on `Object.prototype` or
160
+ `Function.prototype` sees the walk, takes the host's function, or hands the host
161
+ one of its own. A `Module#evaluate(report:)` `@` path is walked the same way.
162
+
163
+ Which realm's: each realm has its own, made as it is first used, and made anew
164
+ with the realm by `reset` (what was attached there goes as a global's does).
165
+ `HOST_OBJECT` is the host object of the realm the operation runs in — a
166
+ Context's, a `Function` handle's (whatever realm the function it calls is of),
167
+ or, as a host function's result, the realm that host function was attached to —
168
+ and that realm's host object crosses back into Ruby as `HOST_OBJECT`; another
169
+ realm's reads as a plain object. With `host_namespace: "@…"` the namespace has
170
+ one member more, `hostObjectOf(id)`: realm `id`'s host object, for one realm's
171
+ bindings to reach another's (a namespace on the global never has it).
172
+
173
+ Hand it over before any script of the page's runs: `call` reaches the bindings
174
+ by a global name, which a page could otherwise have replaced — and have the
175
+ bindings delete that name as they take it. An embedder linking rusty_racer as a
176
+ library reaches it natively with `rusty_racer::host_object(scope)` instead —
177
+ from its realm init hook, say, to hand it to bindings baked into a snapshot
178
+ before any script runs. The object has no prototype, so V8 keeps it as a
179
+ dictionary: bindings that call through it in a hot loop do better to keep the
180
+ functions they read off it.
181
+
126
182
  ### Releasing handles
127
183
 
128
184
  A `Function`, `Script` or `Module` handle is released when it is `#dispose`d —
@@ -186,6 +242,53 @@ exception"), at once for a throw and as it settles for an `await`:
186
242
 
187
243
  Classic `<script>`s work the same way: `ctx.compile("1 + 1").run` # => 2.
188
244
 
245
+ The resolve block is handed each request's import attributes too —
246
+ `{ |specifier, referrer, attributes| … }`, `{"type" => "json"}` for
247
+ `import x from "./x.json" with { type: "json" }` — and `app.requests` lists them
248
+ without linking: `[["./dep.js", {}]]`.
249
+
250
+ A module whose value the host makes — HTML's JSON, CSS and text module scripts —
251
+ is a *synthetic* module exporting `default` alone:
252
+
253
+ ```ruby
254
+ ctx.synthetic_module("some text", filename: "/a.txt") # a Ruby value
255
+ ctx.synthetic_module(json: '{"a": 1}', filename: "/a.json") # %JSON.parse% (ParseError if it is no JSON)
256
+ ctx.synthetic_module(call: ["@makeSheet", css], filename: "/a.css") # what a function returns
257
+ ```
258
+
259
+ `import()` is the host's to answer, now or later: the isolate's
260
+ `dynamic_import_handler` is handed a `RustyRacer::DynamicImport` per call —
261
+ `specifier`, `attributes`, `referrer` (the calling script's filename),
262
+ `host_defined` (the value that script was compiled with: `compile` /
263
+ `compile_module` take `host_defined:`, a primitive, to tell scripts of one name
264
+ apart) and `context` (the realm it was called in) — and its promise waits until
265
+ the host settles it, on whatever later turn its fetch lands:
266
+
267
+ ```ruby
268
+ iso.dynamic_import_handler = ->(import) { pending << import } # start a fetch, return
269
+ # … later:
270
+ import.resolve(mod) { |specifier, referrer, attributes| dep } # linked (by the block) and evaluated
271
+ import.reject_with("@steps.typeError", "no such module") # rejected with what a function makes
272
+ import.reject(42) # … or with a value
273
+ ```
274
+
275
+ Whatever leaves the handler other than by returning — an exception, an
276
+ `Interrupt` or a `Timeout` too — rejects the `import()` with its message (unless
277
+ the handler settled it first) and goes no further. A module still evaluating
278
+ (an `import()` of itself settled from inside its handler) rejects as busy:
279
+ settle such a one on a later turn. An evaluation that is terminated raises
280
+ `ScriptTerminatedError` from `resolve` and leaves the promise pending. Code an
281
+ `eval` / `new Function` compiles carries its script's `host_defined:` value, but
282
+ V8 caches it by source, so another script's identical source may keep the first
283
+ one's.
284
+
285
+ `dynamic_import_resolver = ->(specifier, referrer, context) { module }` remains,
286
+ answering each `import()` at once. A realm's `import.meta` gets its `url`; a
287
+ namespace's `setImportMetaHandler(fn)` adds whatever else the host makes —
288
+ `fn(meta, url, hostDefined)` is called as a module of that realm first reads it,
289
+ `import.meta.resolve` for one.
290
+
291
+
189
292
  ### Resource limits
190
293
 
191
294
  An isolate can cap untrusted code on both axes. Each limit terminates the
@@ -279,7 +382,7 @@ Also available:
279
382
  never fires. It drains every pending task without blocking, then a microtask
280
383
  checkpoint; with nothing pending it is a no-op. The callbacks are ordinary JS,
281
384
  so the pump is capped by `timeout_ms` like an `eval`.
282
- - **`Isolate#terminate`**, **`Isolate#dynamic_import_resolver=`**,
385
+ - **`Isolate#terminate`**, **`Isolate#dynamic_import_handler=`** / **`#dynamic_import_resolver=`**,
283
386
  **`Context#reset`** (below), and **`Platform.set_flags!`**.
284
387
 
285
388
  ### `Context#reset`
@@ -5,7 +5,7 @@
5
5
  # links and loads with no custom archive.
6
6
  [package]
7
7
  name = "rusty_racer"
8
- version = "0.2.7"
8
+ version = "0.2.9"
9
9
  edition = "2024"
10
10
  publish = false
11
11