@@ -23,7 +23,6 @@ namespace url { |
23 | 23 |
|
24 | 24 | using v8::CFunction; |
25 | 25 | using v8::Context; |
26 | | -using v8::FastApiCallbackOptions; |
27 | 26 | using v8::FunctionCallbackInfo; |
28 | 27 | using v8::HandleScope; |
29 | 28 | using v8::Isolate; |
@@ -323,45 +322,38 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) { |
323 | 322 | args.GetReturnValue().Set(can_parse); |
324 | 323 | } |
325 | 324 |
|
326 | | -bool BindingData::FastCanParse( |
327 | | - Local<Value> receiver, |
328 | | - Local<Value> input, |
329 | | - // NOLINTNEXTLINE(runtime/references) This is V8 api. |
330 | | - FastApiCallbackOptions& options) { |
| 325 | +// canParseFast() is a V8 fast API call that returns a value. V8 cannot lazily |
| 326 | +// deoptimize the optimized caller of such a call (it aborts instead), so |
| 327 | +// neither the fast callbacks nor their slow fallback may allocate on the JS |
| 328 | +// heap, flatten a string, call into JavaScript or otherwise trigger a GC. |
| 329 | +// They only accept sequential one-byte strings and return -1 when the input |
| 330 | +// needs the regular canParse() binding (non-ASCII input, cons/two-byte |
| 331 | +// strings, ...); JavaScript then calls that binding separately. |
| 332 | +void BindingData::CanParseFastFallback( |
| 333 | + const FunctionCallbackInfo<Value>& args) { |
| 334 | + args.GetReturnValue().Set(-1); |
| 335 | +} |
| 336 | + |
| 337 | +int32_t BindingData::FastCanParse(Local<Value> receiver, |
| 338 | + const v8::FastOneByteString& input) { |
331 | 339 | TRACK_V8_FAST_API_CALL("url.canParse"); |
332 | | - auto isolate = options.isolate; |
333 | | - HandleScope handleScope(isolate); |
334 | | - Local<String> str; |
335 | | - if (!input->ToString(isolate->GetCurrentContext()).ToLocal(&str)) { |
336 | | - return false; |
337 | | - } |
338 | | - Utf8Value utf8(isolate, str); |
339 | | - return ada::can_parse(utf8.ToStringView()); |
| 340 | + // Latin-1 is only UTF-8 compatible for ASCII. |
| 341 | + if (!simdutf::validate_ascii(input.data, input.length)) return -1; |
| 342 | + return ada::can_parse(std::string_view(input.data, input.length)) ? 1 : 0; |
340 | 343 | } |
341 | 344 |
|
342 | | -bool BindingData::FastCanParseWithBase( |
343 | | - Local<Value> receiver, |
344 | | - Local<Value> input, |
345 | | - Local<Value> base, |
346 | | - // NOLINTNEXTLINE(runtime/references) This is V8 api. |
347 | | - FastApiCallbackOptions& options) { |
| 345 | +int32_t BindingData::FastCanParseWithBase(Local<Value> receiver, |
| 346 | + const v8::FastOneByteString& input, |
| 347 | + const v8::FastOneByteString& base) { |
348 | 348 | TRACK_V8_FAST_API_CALL("url.canParse.withBase"); |
349 | | - auto isolate = options.isolate; |
350 | | - HandleScope handleScope(isolate); |
351 | | - auto context = isolate->GetCurrentContext(); |
352 | | - Local<String> input_str; |
353 | | - if (!input->ToString(context).ToLocal(&input_str)) { |
354 | | - return false; |
355 | | - } |
356 | | - Local<String> base_str; |
357 | | - if (!base->ToString(context).ToLocal(&base_str)) { |
358 | | - return false; |
359 | | - } |
360 | | - Utf8Value input_utf8(isolate, input_str); |
361 | | - Utf8Value base_utf8(isolate, base_str); |
362 | | - |
363 | | - auto base_view = base_utf8.ToStringView(); |
364 | | - return ada::can_parse(input_utf8.ToStringView(), &base_view); |
| 349 | + if (!simdutf::validate_ascii(input.data, input.length) || |
| 350 | + !simdutf::validate_ascii(base.data, base.length)) { |
| 351 | + return -1; |
| 352 | + } |
| 353 | + std::string_view base_view(base.data, base.length); |
| 354 | + return ada::can_parse(std::string_view(input.data, input.length), &base_view) |
| 355 | + ? 1 |
| 356 | + : 0; |
365 | 357 | } |
366 | 358 |
|
367 | 359 | CFunction BindingData::fast_can_parse_methods_[] = { |
@@ -589,8 +581,12 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, |
589 | 581 | SetMethod(isolate, target, "parse", Parse); |
590 | 582 | SetMethod(isolate, target, "pathToFileURL", PathToFileURL); |
591 | 583 | SetMethod(isolate, target, "update", Update); |
592 | | - SetFastMethodNoSideEffect( |
593 | | - isolate, target, "canParse", CanParse, {fast_can_parse_methods_, 2}); |
| 584 | + SetMethodNoSideEffect(isolate, target, "canParse", CanParse); |
| 585 | + SetFastMethodNoSideEffect(isolate, |
| 586 | + target, |
| 587 | + "canParseFast", |
| 588 | + CanParseFastFallback, |
| 589 | + {fast_can_parse_methods_, 2}); |
594 | 590 | } |
595 | 591 |
|
596 | 592 | void BindingData::CreatePerContextProperties(Local<Object> target, |
@@ -611,6 +607,7 @@ void BindingData::RegisterExternalReferences( |
611 | 607 | registry->Register(PathToFileURL); |
612 | 608 | registry->Register(Update); |
613 | 609 | registry->Register(CanParse); |
| 610 | + registry->Register(CanParseFastFallback); |
614 | 611 | for (const CFunction& method : fast_can_parse_methods_) { |
615 | 612 | registry->Register(method); |
616 | 613 | } |
|