|
31 | 31 | #include <thread>
|
32 | 32 | #include "File.h"
|
33 | 33 | #include "ModuleBinding.h"
|
| 34 | +#include "URLImpl.h" |
| 35 | +#include "URLSearchParamsImpl.h" |
34 | 36 |
|
35 | 37 | #ifdef APPLICATION_IN_DEBUG
|
36 | 38 | // #include "NetworkDomainCallbackHandlers.h"
|
@@ -519,6 +521,9 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
|
519 | 521 | globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__runOnMainThread"), FunctionTemplate::New(isolate, CallbackHandlers::RunOnMainThreadCallback));
|
520 | 522 | globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__postFrameCallback"), FunctionTemplate::New(isolate, CallbackHandlers::PostFrameCallback));
|
521 | 523 | globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__removeFrameCallback"), FunctionTemplate::New(isolate, CallbackHandlers::RemoveFrameCallback));
|
| 524 | + globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "URL"), URLImpl::GetCtor(isolate)); |
| 525 | + globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "URLSearchParams"), URLSearchParamsImpl::GetCtor(isolate)); |
| 526 | + |
522 | 527 | /*
|
523 | 528 | * Attach `Worker` object constructor only to the main thread (isolate)'s global object
|
524 | 529 | * Workers should not be created from within other Workers, for now
|
@@ -586,10 +591,78 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
|
586 | 591 |
|
587 | 592 | Local<Context> context = Context::New(isolate, nullptr, globalTemplate);
|
588 | 593 |
|
| 594 | + auto blob_methods = R"js( |
| 595 | + const BLOB_STORE = new Map(); |
| 596 | + URL.createObjectURL = function (object, options = null) { |
| 597 | + try { |
| 598 | + if (object instanceof Blob || object instanceof File) { |
| 599 | + const id = java.util.UUID.randomUUID().toString(); |
| 600 | + const ret = `blob:nativescript/${id}`; |
| 601 | + BLOB_STORE.set(ret, { |
| 602 | + blob: object, |
| 603 | + type: object?.type, |
| 604 | + ext: options?.ext, |
| 605 | + }); |
| 606 | + return ret; |
| 607 | + } |
| 608 | + } catch (error) { |
| 609 | + return null; |
| 610 | + } |
| 611 | + return null; |
| 612 | + }; |
| 613 | + URL.revokeObjectURL = function (url) { |
| 614 | + BLOB_STORE.delete(url); |
| 615 | + }; |
| 616 | + const InternalAccessor = class {}; |
| 617 | + InternalAccessor.getData = function (url) { |
| 618 | + return BLOB_STORE.get(url); |
| 619 | + }; |
| 620 | + URL.InternalAccessor = InternalAccessor; |
| 621 | + Object.defineProperty(URL.prototype, 'searchParams', { |
| 622 | + get() { |
| 623 | + if (this._searchParams == null) { |
| 624 | + this._searchParams = new URLSearchParams(this.search); |
| 625 | + Object.defineProperty(this._searchParams, '_url', { |
| 626 | + enumerable: false, |
| 627 | + writable: false, |
| 628 | + value: this, |
| 629 | + }); |
| 630 | + this._searchParams._append = this._searchParams.append; |
| 631 | + this._searchParams.append = function (name, value) { |
| 632 | + this._append(name, value); |
| 633 | + this._url.search = this.toString(); |
| 634 | + }; |
| 635 | + this._searchParams._delete = this._searchParams.delete; |
| 636 | + this._searchParams.delete = function (name) { |
| 637 | + this._delete(name); |
| 638 | + this._url.search = this.toString(); |
| 639 | + }; |
| 640 | + this._searchParams._set = this._searchParams.set; |
| 641 | + this._searchParams.set = function (name, value) { |
| 642 | + this._set(name, value); |
| 643 | + this._url.search = this.toString(); |
| 644 | + }; |
| 645 | + this._searchParams._sort = this._searchParams.sort; |
| 646 | + this._searchParams.sort = function () { |
| 647 | + this._sort(); |
| 648 | + this._url.search = this.toString(); |
| 649 | + }; |
| 650 | + } |
| 651 | + return this._searchParams; |
| 652 | + }, |
| 653 | + }); |
| 654 | + )js"; |
| 655 | + |
| 656 | + |
589 | 657 | auto global = context->Global();
|
590 | 658 |
|
591 | 659 | v8::Context::Scope contextScope{context};
|
592 | 660 |
|
| 661 | + v8::Local<v8::Script> script; |
| 662 | + v8::Script::Compile(context, ArgConverter::ConvertToV8String(isolate, blob_methods)).ToLocal(&script); |
| 663 | + |
| 664 | + v8::Local<v8::Value> out; |
| 665 | + script->Run(context).ToLocal(&out); |
593 | 666 | m_objectManager->Init(isolate);
|
594 | 667 |
|
595 | 668 | m_module.Init(isolate, callingDir);
|
|
0 commit comments