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 +4 -4
- data/CHANGELOG.md +87 -0
- data/README.md +104 -1
- data/ext/rusty_racer/Cargo.toml +1 -1
- data/ext/rusty_racer/src/lib.rs +1022 -216
- data/ext/rusty_racer/src/marshal.rs +76 -1
- data/ext/rusty_racer/src/ops.rs +403 -34
- data/lib/rusty_racer/version.rb +1 -1
- data/lib/rusty_racer.rb +137 -22
- metadata +2 -2
data/lib/rusty_racer.rb
CHANGED
|
@@ -91,31 +91,69 @@ module RustyRacer
|
|
|
91
91
|
# the microtask queue when the outermost eval/call/run/evaluate completes
|
|
92
92
|
# (the standard embedder contract); :explicit drains only on
|
|
93
93
|
# #perform_microtask_checkpoint.
|
|
94
|
+
# host_namespace names the object every realm gets rusty_racer's own
|
|
95
|
+
# functions in (drainMicrotasks, contextGlobal, contextOf, ...): a property
|
|
96
|
+
# of the global (`'MiniRacer'`), or, as `'@MiniRacer'`, of the realm's host
|
|
97
|
+
# object, which no script of a page's can reach (see HOST_OBJECT).
|
|
94
98
|
def self.new(host_namespace: nil, snapshot: nil, timeout_ms: 0, memory_limit: 0, microtasks: :auto)
|
|
95
99
|
unless %i[auto explicit].include?(microtasks)
|
|
96
100
|
raise ArgumentError, "microtasks must be :auto or :explicit, got #{microtasks.inspect}"
|
|
97
101
|
end
|
|
102
|
+
raise ArgumentError, 'host_namespace: "@" names no property of the host object' if host_namespace == '@'
|
|
98
103
|
|
|
99
104
|
_new(host_namespace, snapshot, timeout_ms, memory_limit, microtasks == :explicit)
|
|
100
105
|
end
|
|
101
106
|
|
|
102
|
-
# ->(
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
# the
|
|
106
|
-
#
|
|
107
|
-
#
|
|
108
|
-
#
|
|
109
|
-
#
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
|
|
107
|
+
# ->(import) { ... } for JS import(): handed each import()'s
|
|
108
|
+
# RustyRacer::DynamicImport — the specifier, its import attributes, the
|
|
109
|
+
# referring script's name and host-defined value, the realm it was called in
|
|
110
|
+
# — which the handler settles now or later (a fetch begun now, settled on a
|
|
111
|
+
# later task): #resolve with a Module, #reject / #reject_with with a value.
|
|
112
|
+
# Until then the import()'s promise is pending. Whatever leaves the handler
|
|
113
|
+
# other than by returning — any exception, an Interrupt or a Timeout
|
|
114
|
+
# included — rejects it with an Error of its message, unless it settled it
|
|
115
|
+
# first, and goes no further; nil clears the handler, an import() then
|
|
116
|
+
# rejected. The native side keeps the proc alive (in the isolate's roots
|
|
117
|
+
# array, marked by its wrappers), so `dispose` drops whatever it captured.
|
|
118
|
+
def dynamic_import_handler=(handler)
|
|
119
|
+
_set_dynamic_import_handler(handler)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# ->(specifier, referrer_url, context) { Module } for JS import(), answered at
|
|
123
|
+
# once: the dynamic_import_handler that resolves each import() with the
|
|
124
|
+
# Module the resolver returns — linked through the resolver too (a static
|
|
125
|
+
# import met while linking it asks it, with the same realm), and evaluated —
|
|
126
|
+
# or rejects it where the resolver returns nil. nil clears it.
|
|
114
127
|
def dynamic_import_resolver=(resolver)
|
|
115
|
-
|
|
128
|
+
self.dynamic_import_handler = resolver && lambda do |import|
|
|
129
|
+
context = import.context
|
|
130
|
+
mod = resolver.call(import.specifier, import.referrer.to_s, context)
|
|
131
|
+
# (…raised: the import rejected with an Error of the realm's own, natively)
|
|
132
|
+
raise Error, "import() was not resolved to a module (#{import.specifier.inspect})" unless mod
|
|
133
|
+
|
|
134
|
+
import.resolve(mod) {|specifier, referrer| resolver.call(specifier, referrer, context) }
|
|
135
|
+
end
|
|
116
136
|
end
|
|
117
137
|
end
|
|
118
138
|
|
|
139
|
+
# HOST_OBJECT (defined natively): an argument that crosses into JS as the
|
|
140
|
+
# realm's HOST OBJECT — an object with no prototype that no global holds, which
|
|
141
|
+
# the host and the realm's own bindings share and no other script can reach.
|
|
142
|
+
# Every path the host names (Context#attach / #attach_many / #call /
|
|
143
|
+
# #call_void / #function, Module#evaluate's report:, Isolate.new's
|
|
144
|
+
# host_namespace:) starts from it when it begins with `@`:
|
|
145
|
+
#
|
|
146
|
+
# ctx.attach('@fetch', ->(url) { ... }) # host.fetch, no global
|
|
147
|
+
# ctx.call('installBindings', RustyRacer::HOST_OBJECT)
|
|
148
|
+
# step = ctx.function('@steps.query') # host.steps.query
|
|
149
|
+
#
|
|
150
|
+
# An `@` path walks own properties alone. HOST_OBJECT is the host object of
|
|
151
|
+
# the realm the operation runs in (a Context's, a Function handle's, a host
|
|
152
|
+
# function's own, for its result), and that object crosses back as
|
|
153
|
+
# HOST_OBJECT. Each realm has its own, made as it is first used; a reset makes
|
|
154
|
+
# the realm, and so its host object, anew. Hand it to the bindings before any
|
|
155
|
+
# script of a page's runs, by a name they delete as they take it.
|
|
156
|
+
|
|
119
157
|
# A v8::Context (realm) handed out by an Isolate: where JS actually runs.
|
|
120
158
|
class Context
|
|
121
159
|
# `timeout_ms` (0 = the isolate default) caps this eval; `filename` names the
|
|
@@ -145,8 +183,14 @@ module RustyRacer
|
|
|
145
183
|
# Compile a classic <script>; returns a RustyRacer::Script to #run.
|
|
146
184
|
# cached_data:/produce_cache: are the bytecode cache (see #compile_module).
|
|
147
185
|
# eager: (see #compile_module).
|
|
148
|
-
|
|
149
|
-
|
|
186
|
+
# host_defined: a primitive (Integer, Float, String, true, false; not a
|
|
187
|
+
# Symbol) its ScriptOrigin carries, which an import() it makes is handed
|
|
188
|
+
# (DynamicImport#host_defined) — how a host tells one referring script from
|
|
189
|
+
# another of the same name. Code an `eval` or `new Function` of it compiles
|
|
190
|
+
# carries its value too — but V8 caches such code by its source, so the
|
|
191
|
+
# same source evaluated by another script may keep the first one's.
|
|
192
|
+
def compile(source, filename: '<compile>', cached_data: nil, produce_cache: false, eager: false, host_defined: nil)
|
|
193
|
+
_compile(source, filename, cached_data, produce_cache, eager, host_defined)
|
|
150
194
|
end
|
|
151
195
|
|
|
152
196
|
# Compile an ES module; returns a RustyRacer::Module to instantiate/evaluate.
|
|
@@ -163,14 +207,54 @@ module RustyRacer
|
|
|
163
207
|
# inner functions, so eager: alone doesn't change the cache yet — it's a
|
|
164
208
|
# forward-looking, semantically-correct switch. For a cache that DOES carry
|
|
165
209
|
# inner functions, run the script/module and call #create_code_cache.
|
|
166
|
-
|
|
167
|
-
|
|
210
|
+
#
|
|
211
|
+
# host_defined: (see #compile) — handed to an import() the module makes, and
|
|
212
|
+
# to its realm's import-meta handler (NS.setImportMetaHandler).
|
|
213
|
+
def compile_module(source, filename: '<compile_module>', cached_data: nil, produce_cache: false, eager: false, host_defined: nil)
|
|
214
|
+
_compile_module(source, filename, cached_data, produce_cache, eager, host_defined)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# A synthetic module (V8 CreateSyntheticModule) exporting `default` alone,
|
|
218
|
+
# under `filename` — what HTML makes of a JSON, CSS or text module script —
|
|
219
|
+
# its value one of:
|
|
220
|
+
#
|
|
221
|
+
# synthetic_module('text', filename: '/a.txt') # a Ruby value, marshalled
|
|
222
|
+
# synthetic_module(json: '{"a":1}', filename: '/a.json') # %JSON.parse% of a JSON text (ECMA-262 JSON modules)
|
|
223
|
+
# synthetic_module(call: ['@makeSheet', css], filename: '/a.css')
|
|
224
|
+
# # what the function at a path (as #call names one) returns
|
|
225
|
+
#
|
|
226
|
+
# A JSON text that is none raises RustyRacer::ParseError, as a module's
|
|
227
|
+
# SyntaxError does; a function that throws, RustyRacer::RuntimeError.
|
|
228
|
+
# Linked and evaluated as any module is; it has no requests.
|
|
229
|
+
def synthetic_module(*value, filename: '<synthetic_module>', json: nil, call: nil)
|
|
230
|
+
given = [!value.empty?, !json.nil?, !call.nil?].count(true)
|
|
231
|
+
raise ArgumentError, 'synthetic_module takes one of a value, json: or call:' unless given == 1 && value.size <= 1
|
|
232
|
+
|
|
233
|
+
if json
|
|
234
|
+
_synthetic_module(filename, 1, json, [])
|
|
235
|
+
elsif call
|
|
236
|
+
path, *args = call
|
|
237
|
+
_synthetic_module(filename, 2, path, args)
|
|
238
|
+
else
|
|
239
|
+
_synthetic_module(filename, 0, value.first, [])
|
|
240
|
+
end
|
|
168
241
|
end
|
|
169
242
|
end
|
|
170
243
|
|
|
244
|
+
# A compiled (or synthetic) ES module. Defined natively besides: #requests ->
|
|
245
|
+
# [[specifier, attributes], ...], what the module imports (V8
|
|
246
|
+
# GetModuleRequests), in source order, each with its frozen import attributes —
|
|
247
|
+
# the requests linking it asks its resolve block for, one by one; #script_id,
|
|
248
|
+
# the id V8's stack frames name its code by (StackFrame::GetScriptId) — how an
|
|
249
|
+
# embedder walking the stack tells which module is running — nil for a
|
|
250
|
+
# synthetic or an errored module. Modules V8's compilation cache shares share
|
|
251
|
+
# it: two compiles of one source under one filename answer the same id
|
|
252
|
+
# (Script#script_id, a classic script's, likewise).
|
|
171
253
|
class Module
|
|
172
|
-
# instantiate { |specifier, referrer_url| dependency_module } —
|
|
173
|
-
# resolves each import to an already-compiled Module
|
|
254
|
+
# instantiate { |specifier, referrer_url, attributes| dependency_module } —
|
|
255
|
+
# the block resolves each import to an already-compiled Module; attributes
|
|
256
|
+
# is the request's import attributes, {'type' => 'json'} (a request with
|
|
257
|
+
# none: one frozen empty Hash). Returns self.
|
|
174
258
|
def instantiate(&block)
|
|
175
259
|
raise ArgumentError, 'instantiate requires a resolver block' unless block
|
|
176
260
|
|
|
@@ -180,9 +264,9 @@ module RustyRacer
|
|
|
180
264
|
|
|
181
265
|
# Evaluate the instantiated module. Its evaluation's rejection — a top-level throw, a top-level await's — raises
|
|
182
266
|
# (RustyRacer::RuntimeError, as far as the drain after it settles), or, `report:` naming a function of its context's
|
|
183
|
-
# global, is that function's to report: called with the rejection's
|
|
184
|
-
# returns, a top-level await's as it settles — and nothing raised (HTML
|
|
185
|
-
# report the exception).
|
|
267
|
+
# global (an `@` path: on its host object, HOST_OBJECT), is that function's to report: called with the rejection's
|
|
268
|
+
# reason — a top-level throw's before this returns, a top-level await's as it settles — and nothing raised (HTML
|
|
269
|
+
# "run a module script": upon rejection, report the exception).
|
|
186
270
|
def evaluate(report: nil)
|
|
187
271
|
_evaluate(report&.to_s)
|
|
188
272
|
end
|
|
@@ -193,4 +277,35 @@ module RustyRacer
|
|
|
193
277
|
_status.to_sym
|
|
194
278
|
end
|
|
195
279
|
end
|
|
280
|
+
|
|
281
|
+
# An import() waiting for its host — handed to Isolate#dynamic_import_handler.
|
|
282
|
+
# #specifier, #attributes ({'type' => 'json'}), #referrer (the name of the
|
|
283
|
+
# script that called import(), or nil), #host_defined (that script's
|
|
284
|
+
# host_defined:, or nil) and #context (the realm it was called in, whose
|
|
285
|
+
# modules it is settled with) say what was asked; #resolve, #reject and
|
|
286
|
+
# #reject_with settle it — now, inside the handler, or on a later task — each
|
|
287
|
+
# answering whether it was still waiting (false once settled, or once its
|
|
288
|
+
# realm was reset or disposed). Let go of unsettled, its promise is let go of
|
|
289
|
+
# too, pending.
|
|
290
|
+
class DynamicImport
|
|
291
|
+
# resolve(module) { |specifier, referrer_url, attributes| dependency } —
|
|
292
|
+
# fulfil the import() with `module`'s namespace once it is evaluated:
|
|
293
|
+
# linked first, where it is not yet, by the block (as Module#instantiate's
|
|
294
|
+
# is), then evaluated (its top-level await awaited). A link or evaluation
|
|
295
|
+
# error — a block that raised included — rejects the import() with it. A
|
|
296
|
+
# module still linking or evaluating (an import() of itself, settled at once
|
|
297
|
+
# from inside the handler) rejects it as busy: settle such a one later, from
|
|
298
|
+
# a later turn. An evaluation terminated (a timeout, Isolate#terminate)
|
|
299
|
+
# raises ScriptTerminatedError and leaves the import() pending for good.
|
|
300
|
+
def resolve(mod, &block)
|
|
301
|
+
_resolve(mod, block)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# #reject(value): reject the import() with `value`, marshalled into its realm
|
|
305
|
+
# as any argument is (nil, true/false, numbers, Strings, Arrays, Hashes, …,
|
|
306
|
+
# HOST_OBJECT) — a plain value: an Error of the realm's own is #reject_with's.
|
|
307
|
+
# #reject_with(path, *args): reject it with what the function at `path` (as
|
|
308
|
+
# Context#call names one, in its realm) returns — `'@steps.typeError'`, say,
|
|
309
|
+
# for an error of the host's own making — or with what it throws.
|
|
310
|
+
end
|
|
196
311
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rusty_racer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Keita Urashima
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rb_sys
|