rusty_racer 0.2.8 → 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.
data/lib/rusty_racer.rb CHANGED
@@ -104,20 +104,35 @@ module RustyRacer
104
104
  _new(host_namespace, snapshot, timeout_ms, memory_limit, microtasks == :explicit)
105
105
  end
106
106
 
107
- # ->(specifier, referrer_url, context) { Module } for JS import(). |context|
108
- # is the realm import() actually fired in (the Context, not just its id), so
109
- # an import() from an extra realm resolves/compiles in THAT realm rather than
110
- # the main one — return e.g. context.compile_module(src, filename: specifier).
111
- # The block may return a merely compiled Module: linking and evaluation are
112
- # the binding's job (V8's host contract), and static imports met while linking
113
- # resolve through this same block (also with the realm as the 3rd arg).
114
- # (Module#instantiate's own resolve block keeps its 2-arg form.)
115
- # The native side keeps the proc alive itself (in the isolate's roots array,
116
- # marked by its wrappers), so it is NOT held in an ivar here: that second
117
- # reference would outlive `dispose`, which exists to drop whatever the resolver
118
- # captured the moment the isolate can no longer call it.
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.
119
127
  def dynamic_import_resolver=(resolver)
120
- _set_dynamic_import_resolver(resolver)
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
121
136
  end
122
137
  end
123
138
 
@@ -168,8 +183,14 @@ module RustyRacer
168
183
  # Compile a classic <script>; returns a RustyRacer::Script to #run.
169
184
  # cached_data:/produce_cache: are the bytecode cache (see #compile_module).
170
185
  # eager: (see #compile_module).
171
- def compile(source, filename: '<compile>', cached_data: nil, produce_cache: false, eager: false)
172
- _compile(source, filename, cached_data, produce_cache, eager)
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)
173
194
  end
174
195
 
175
196
  # Compile an ES module; returns a RustyRacer::Module to instantiate/evaluate.
@@ -186,14 +207,54 @@ module RustyRacer
186
207
  # inner functions, so eager: alone doesn't change the cache yet — it's a
187
208
  # forward-looking, semantically-correct switch. For a cache that DOES carry
188
209
  # inner functions, run the script/module and call #create_code_cache.
189
- def compile_module(source, filename: '<compile_module>', cached_data: nil, produce_cache: false, eager: false)
190
- _compile_module(source, filename, cached_data, produce_cache, eager)
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
191
241
  end
192
242
  end
193
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).
194
253
  class Module
195
- # instantiate { |specifier, referrer_url| dependency_module } — the block
196
- # resolves each import to an already-compiled Module. Returns self.
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.
197
258
  def instantiate(&block)
198
259
  raise ArgumentError, 'instantiate requires a resolver block' unless block
199
260
 
@@ -216,4 +277,35 @@ module RustyRacer
216
277
  _status.to_sym
217
278
  end
218
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
219
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.8
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-08 00:00:00.000000000 Z
11
+ date: 2026-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys