jstime_core 0.66.0

Another JS Runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
pub(crate) fn get_external_references() -> Vec<v8::ExternalReference> {
    vec![
        v8::ExternalReference {
            function: v8::MapFnTo::map_fn_to(event_target_add_event_listener),
        },
        v8::ExternalReference {
            function: v8::MapFnTo::map_fn_to(event_target_remove_event_listener),
        },
        v8::ExternalReference {
            function: v8::MapFnTo::map_fn_to(event_target_dispatch_event),
        },
        v8::ExternalReference {
            function: v8::MapFnTo::map_fn_to(event_stop_propagation),
        },
        v8::ExternalReference {
            function: v8::MapFnTo::map_fn_to(event_stop_immediate_propagation),
        },
        v8::ExternalReference {
            function: v8::MapFnTo::map_fn_to(event_prevent_default),
        },
    ]
}

pub(crate) fn register_bindings(scope: &mut v8::PinScope, bindings: v8::Local<v8::Object>) {
    // EventTarget methods
    let name = v8::String::new(scope, "eventTargetAddEventListener").unwrap();
    let value = v8::Function::new(scope, event_target_add_event_listener).unwrap();
    bindings.set(scope, name.into(), value.into());

    let name = v8::String::new(scope, "eventTargetRemoveEventListener").unwrap();
    let value = v8::Function::new(scope, event_target_remove_event_listener).unwrap();
    bindings.set(scope, name.into(), value.into());

    let name = v8::String::new(scope, "eventTargetDispatchEvent").unwrap();
    let value = v8::Function::new(scope, event_target_dispatch_event).unwrap();
    bindings.set(scope, name.into(), value.into());

    // Event methods
    let name = v8::String::new(scope, "eventStopPropagation").unwrap();
    let value = v8::Function::new(scope, event_stop_propagation).unwrap();
    bindings.set(scope, name.into(), value.into());

    let name = v8::String::new(scope, "eventStopImmediatePropagation").unwrap();
    let value = v8::Function::new(scope, event_stop_immediate_propagation).unwrap();
    bindings.set(scope, name.into(), value.into());

    let name = v8::String::new(scope, "eventPreventDefault").unwrap();
    let value = v8::Function::new(scope, event_prevent_default).unwrap();
    bindings.set(scope, name.into(), value.into());
}

// EventTarget.addEventListener(type, listener, options)
#[inline]
fn event_target_add_event_listener(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _rv: v8::ReturnValue,
) {
    // args[0] = target (EventTarget instance)
    // args[1] = type (event type string)
    // args[2] = listener (function)
    // args[3] = options (optional)

    if args.length() < 3 {
        return;
    }

    let target = args.get(0);
    let type_arg = args.get(1);
    let listener = args.get(2);

    // Validate inputs early - fast rejection path
    if !listener.is_function() {
        return;
    }

    let target_obj = match v8::Local::<v8::Object>::try_from(target) {
        Ok(obj) => obj,
        Err(_) => return,
    };

    let listener_func = v8::Local::<v8::Function>::try_from(listener).unwrap();

    // Convert type to a V8 string - fast path if already a string
    let type_key = if type_arg.is_string() {
        v8::Local::<v8::String>::try_from(type_arg).unwrap()
    } else {
        v8::tc_scope!(let tc, scope);
        match type_arg.to_string(tc) {
            Some(s) => s,
            None => return,
        }
    };

    // Get cached __listeners__ key string from the isolate state
    // Acquire cache once and release quickly to minimize lock contention
    let listeners_key = {
        let isolate_state = crate::isolate_state::IsolateState::get(scope);
        let string_cache_ref = isolate_state.borrow().string_cache.clone();
        let mut cache = string_cache_ref.borrow_mut();
        crate::get_or_create_cached_string!(scope, cache, listeners, "__listeners__")
    };

    // Get the listeners map - should always exist since EventTarget constructor creates it
    let listeners_map = match target_obj.get(scope, listeners_key.into()) {
        Some(val) if val.is_map() => v8::Local::<v8::Map>::try_from(val).unwrap(),
        _ => {
            // Fallback: create new map if it doesn't exist (shouldn't happen normally)
            let new_map = v8::Map::new(scope);
            target_obj.set(scope, listeners_key.into(), new_map.into());
            new_map
        }
    };

    // Get or create the array of listeners for this event type
    match listeners_map.get(scope, type_key.into()) {
        Some(arr) if arr.is_array() => {
            // Fast path: array exists, just append
            let array = v8::Local::<v8::Array>::try_from(arr).unwrap();
            let length = array.length();
            array.set_index(scope, length, listener_func.into());
        }
        _ => {
            // Create new array for this event type with room for growth
            // Pre-allocate 4 slots to reduce reallocations for common cases
            // where multiple listeners are added to the same event type
            let array = v8::Array::new(scope, 4);
            array.set_index(scope, 0, listener_func.into());
            listeners_map.set(scope, type_key.into(), array.into());
        }
    }
}

// EventTarget.removeEventListener(type, listener, options)
#[inline]
fn event_target_remove_event_listener(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _rv: v8::ReturnValue,
) {
    if args.length() < 3 {
        return;
    }

    let target = args.get(0);
    let type_arg = args.get(1);
    let listener = args.get(2);

    // Validate inputs early
    if !listener.is_function() {
        return;
    }

    let target_obj = match v8::Local::<v8::Object>::try_from(target) {
        Ok(obj) => obj,
        Err(_) => return,
    };

    // Convert type to a V8 string - fast path if already a string
    let type_key = if type_arg.is_string() {
        v8::Local::<v8::String>::try_from(type_arg).unwrap()
    } else {
        v8::tc_scope!(let tc, scope);
        match type_arg.to_string(tc) {
            Some(s) => s,
            None => return,
        }
    };

    // Get cached __listeners__ key string from the isolate state
    // Acquire cache once and release quickly
    let listeners_key = {
        let isolate_state = crate::isolate_state::IsolateState::get(scope);
        let string_cache_ref = isolate_state.borrow().string_cache.clone();
        let mut cache = string_cache_ref.borrow_mut();
        crate::get_or_create_cached_string!(scope, cache, listeners, "__listeners__")
    };

    // Early return if no listeners exist
    let listeners_map = match target_obj.get(scope, listeners_key.into()) {
        Some(val) if val.is_map() => v8::Local::<v8::Map>::try_from(val).unwrap(),
        _ => return,
    };

    let listeners_array = match listeners_map.get(scope, type_key.into()) {
        Some(val) if val.is_array() => v8::Local::<v8::Array>::try_from(val).unwrap(),
        _ => return,
    };

    // Find and remove the listener
    let listener_func = v8::Local::<v8::Function>::try_from(listener).unwrap();
    let length = listeners_array.length();

    // Pre-allocate new array with appropriate size
    let new_array = v8::Array::new(scope, 0);
    let mut new_index = 0;
    let mut removed = false;

    for i in 0..length {
        if let Some(item) = listeners_array.get_index(scope, i)
            && item.is_function()
        {
            let item_func = v8::Local::<v8::Function>::try_from(item).unwrap();
            // Compare function references - skip only the first match
            if !removed && item_func.strict_equals(listener_func.into()) {
                removed = true;
                continue;
            }
            new_array.set_index(scope, new_index, item);
            new_index += 1;
        }
    }

    // Update the map with the new array
    listeners_map.set(scope, type_key.into(), new_array.into());
}

// EventTarget.dispatchEvent(event)
// NOTE: This function is no longer the primary dispatch path.
// Event dispatching now happens in JavaScript (event.js) for better performance.
// This function is kept for backwards compatibility and snapshot support.
#[inline]
fn event_target_dispatch_event(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut rv: v8::ReturnValue,
) {
    if args.length() < 2 {
        rv.set(v8::Boolean::new(scope, true).into());
        return;
    }

    let target = args.get(0);
    let event = args.get(1);

    let event_obj = match v8::Local::<v8::Object>::try_from(event) {
        Ok(obj) => obj,
        Err(_) => {
            rv.set(v8::Boolean::new(scope, true).into());
            return;
        }
    };

    let target_obj = match v8::Local::<v8::Object>::try_from(target) {
        Ok(obj) => obj,
        Err(_) => {
            rv.set(v8::Boolean::new(scope, true).into());
            return;
        }
    };

    // Get all cached strings at once to minimize borrow overhead
    let isolate_state = crate::isolate_state::IsolateState::get(scope);
    let string_cache_ref = isolate_state.borrow().string_cache.clone();
    let mut cache = string_cache_ref.borrow_mut();

    let current_target_key =
        crate::get_or_create_cached_string!(scope, cache, current_target, "__currentTarget__");
    let target_key = crate::get_or_create_cached_string!(scope, cache, target, "__target__");
    let type_key = crate::get_or_create_cached_string!(scope, cache, type_, "type");
    let listeners_key =
        crate::get_or_create_cached_string!(scope, cache, listeners, "__listeners__");
    let stop_immediate_key = crate::get_or_create_cached_string!(
        scope,
        cache,
        stop_immediate_propagation,
        "__stopImmediatePropagation__"
    );
    let default_prevented_key = crate::get_or_create_cached_string!(
        scope,
        cache,
        default_prevented,
        "__defaultPrevented__"
    );

    // Release the cache borrow early
    drop(cache);

    // Set currentTarget on the event
    event_obj.set(scope, current_target_key.into(), target);

    // Set target on the event if not already set
    let existing_target = event_obj.get(scope, target_key.into());
    if existing_target.is_none() || existing_target.unwrap().is_null_or_undefined() {
        event_obj.set(scope, target_key.into(), target);
    }

    // Get event type - use cached type string if available to avoid allocations
    // in the common case where the same event is dispatched multiple times
    let type_str_key = v8::String::new_external_onebyte_static(scope, b"__typeStr__").unwrap();

    let type_key_lookup = if let Some(cached) = event_obj.get(scope, type_str_key.into()) {
        if cached.is_string() {
            v8::Local::<v8::String>::try_from(cached).unwrap()
        } else {
            // Fallback: get type from event and cache it
            let type_val = match event_obj.get(scope, type_key.into()) {
                Some(val) => val,
                None => {
                    rv.set(v8::Boolean::new(scope, true).into());
                    return;
                }
            };

            let type_v8_str_opt = {
                v8::tc_scope!(let tc, scope);
                type_val.to_string(tc)
            };

            let type_v8_str = match type_v8_str_opt {
                Some(s) => s,
                None => {
                    rv.set(v8::Boolean::new(scope, true).into());
                    return;
                }
            };

            // Cache the V8 string on the event object for future dispatches
            event_obj.set(scope, type_str_key.into(), type_v8_str.into());

            type_v8_str
        }
    } else {
        // First dispatch: get type from event and cache it
        let type_val = match event_obj.get(scope, type_key.into()) {
            Some(val) => val,
            None => {
                rv.set(v8::Boolean::new(scope, true).into());
                return;
            }
        };

        let type_v8_str_opt = {
            v8::tc_scope!(let tc, scope);
            type_val.to_string(tc)
        };

        let type_v8_str = match type_v8_str_opt {
            Some(s) => s,
            None => {
                rv.set(v8::Boolean::new(scope, true).into());
                return;
            }
        };

        // Cache the V8 string on the event object for future dispatches
        event_obj.set(scope, type_str_key.into(), type_v8_str.into());

        type_v8_str
    };

    // Get listeners for this event type
    let listeners_map = match target_obj.get(scope, listeners_key.into()) {
        Some(val) if val.is_map() => v8::Local::<v8::Map>::try_from(val).unwrap(),
        _ => {
            // No listeners registered, return early
            rv.set(v8::Boolean::new(scope, true).into());
            return;
        }
    };

    // Use the already-converted type_key_lookup (which is a v8::String)
    let listeners_array = match listeners_map.get(scope, type_key_lookup.into()) {
        Some(val) if val.is_array() => v8::Local::<v8::Array>::try_from(val).unwrap(),
        _ => {
            // No listeners for this event type, return early
            rv.set(v8::Boolean::new(scope, true).into());
            return;
        }
    };

    // Call each listener
    let length = listeners_array.length();
    let event_arg = [event];

    for i in 0..length {
        // Get listener at index i and call it if it's a function
        if let Some(item) = listeners_array.get_index(scope, i)
            && item.is_function()
        {
            let listener_func = v8::Local::<v8::Function>::try_from(item).unwrap();
            listener_func.call(scope, target, &event_arg);

            // Check if immediate propagation was stopped after the call
            if let Some(stop_val) = event_obj.get(scope, stop_immediate_key.into())
                && stop_val.is_true()
            {
                break;
            }
        }
    }

    // Return !defaultPrevented
    let default_prevented = event_obj
        .get(scope, default_prevented_key.into())
        .map(|v| v.is_true())
        .unwrap_or(false);

    rv.set(v8::Boolean::new(scope, !default_prevented).into());
}

// Event.stopPropagation()
fn event_stop_propagation(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _rv: v8::ReturnValue,
) {
    if args.length() < 1 {
        return;
    }

    let event = args.get(0);
    let event_obj = match v8::Local::<v8::Object>::try_from(event) {
        Ok(obj) => obj,
        Err(_) => return,
    };

    // Get cached strings from the isolate state
    let isolate_state = crate::isolate_state::IsolateState::get(scope);
    let string_cache_ref = isolate_state.borrow().string_cache.clone();
    let mut cache = string_cache_ref.borrow_mut();

    let key =
        crate::get_or_create_cached_string!(scope, cache, stop_propagation, "__stopPropagation__");
    let value = v8::Boolean::new(scope, true);
    event_obj.set(scope, key.into(), value.into());
}

// Event.stopImmediatePropagation()
#[inline]
fn event_stop_immediate_propagation(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _rv: v8::ReturnValue,
) {
    if args.length() < 1 {
        return;
    }

    let event = args.get(0);
    let event_obj = match v8::Local::<v8::Object>::try_from(event) {
        Ok(obj) => obj,
        Err(_) => return,
    };

    // Get cached strings from the isolate state
    let isolate_state = crate::isolate_state::IsolateState::get(scope);
    let string_cache_ref = isolate_state.borrow().string_cache.clone();
    let mut cache = string_cache_ref.borrow_mut();

    let key = crate::get_or_create_cached_string!(
        scope,
        cache,
        stop_immediate_propagation,
        "__stopImmediatePropagation__"
    );
    let value = v8::Boolean::new(scope, true);
    event_obj.set(scope, key.into(), value.into());

    // Also stop propagation
    let prop_key =
        crate::get_or_create_cached_string!(scope, cache, stop_propagation, "__stopPropagation__");
    event_obj.set(scope, prop_key.into(), value.into());
}

// Event.preventDefault()
fn event_prevent_default(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    _rv: v8::ReturnValue,
) {
    if args.length() < 1 {
        return;
    }

    let event = args.get(0);
    let event_obj = match v8::Local::<v8::Object>::try_from(event) {
        Ok(obj) => obj,
        Err(_) => return,
    };

    // Get cached strings from the isolate state
    let isolate_state = crate::isolate_state::IsolateState::get(scope);
    let string_cache_ref = isolate_state.borrow().string_cache.clone();
    let mut cache = string_cache_ref.borrow_mut();

    // Only allow preventDefault if cancelable
    let cancelable_key =
        crate::get_or_create_cached_string!(scope, cache, cancelable, "cancelable");
    let is_cancelable = event_obj
        .get(scope, cancelable_key.into())
        .map(|v| v.is_true())
        .unwrap_or(false);

    if is_cancelable {
        let key = crate::get_or_create_cached_string!(
            scope,
            cache,
            default_prevented,
            "__defaultPrevented__"
        );
        let value = v8::Boolean::new(scope, true);
        event_obj.set(scope, key.into(), value.into());
    }
}