Skip to content

Commit ada08e1

Browse files
committed
feat: add URL & URLSearchParams
1 parent 2fae8d0 commit ada08e1

13 files changed

Lines changed: 24260 additions & 28 deletions

File tree

test-app/app/src/main/assets/internal/ts_helpers.js

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@
9292
Array.prototype.slice.call(arguments, 1)
9393
);
9494
} else {
95-
thiz = new (Function.prototype.bind.apply(
95+
thiz = new (Function.prototype.bind.apply(
9696
Extended,
9797
[null].concat(Array.prototype.slice.call(arguments, 1))
9898
))();
9999
}
100100
} else {
101-
thiz = new Extended();
101+
thiz = new Extended();
102102
}
103103
return thiz;
104104
};
@@ -107,17 +107,14 @@
107107
var Extended = extend(thiz);
108108
thiz.__container__ = true;
109109
if (args && args.length > 0) {
110-
if (typeof Extended !== "function") {
111-
thiz = Reflect.construct(
112-
Extended,
113-
[null].concat(args)
114-
);
115-
} else {
116-
thiz = new (Function.prototype.bind.apply(
117-
Extended,
118-
[null].concat(args)
119-
))();
120-
}
110+
if (typeof Extended !== "function") {
111+
thiz = Reflect.construct(Extended, [null].concat(args));
112+
} else {
113+
thiz = new (Function.prototype.bind.apply(
114+
Extended,
115+
[null].concat(args)
116+
))();
117+
}
121118
} else {
122119
thiz = new Extended();
123120
}
@@ -279,14 +276,14 @@
279276
return undefined;
280277
}
281278

282-
globalThis.__prepareHostObject = function (hostObject, jsThis) {
283-
// const prototype = Object.getPrototypeOf(jsThis);
284-
// Object.setPrototypeOf(hostObject, prototype);
285-
Object.defineProperty(hostObject, "super", {
286-
get: () => jsThis["super"],
279+
globalThis.__prepareHostObject = function (hostObject, jsThis) {
280+
// const prototype = Object.getPrototypeOf(jsThis);
281+
// Object.setPrototypeOf(hostObject, prototype);
282+
Object.defineProperty(hostObject, "super", {
283+
get: () => jsThis["super"],
287284
});
288-
};
289-
285+
};
286+
290287
const EXTERNAL_PROP = "[[external]]";
291288
const REFERENCE_PROP_JSC = "[[jsc_reference_info]]";
292289

@@ -331,4 +328,66 @@ globalThis.__prepareHostObject = function (hostObject, jsThis) {
331328
lines[2] = " at extend(native)";
332329
return lines.join("\n");
333330
};
331+
332+
if (globalThis.URL) {
333+
const BLOB_STORE = new Map();
334+
URL.createObjectURL = function (object, options = null) {
335+
try {
336+
if (object instanceof Blob || object instanceof File) {
337+
const id = java.util.UUID.randomUUID().toString();
338+
const ret = `blob:nativescript/${id}`;
339+
BLOB_STORE.set(ret, {
340+
blob: object,
341+
type: object?.type,
342+
ext: options?.ext,
343+
});
344+
return ret;
345+
}
346+
} catch (error) {
347+
return null;
348+
}
349+
return null;
350+
};
351+
URL.revokeObjectURL = function (url) {
352+
BLOB_STORE.delete(url);
353+
};
354+
const InternalAccessor = class {};
355+
InternalAccessor.getData = function (url) {
356+
return BLOB_STORE.get(url);
357+
};
358+
URL.InternalAccessor = InternalAccessor;
359+
Object.defineProperty(URL.prototype, "searchParams", {
360+
get() {
361+
if (this._searchParams == null) {
362+
this._searchParams = new URLSearchParams(this.search);
363+
Object.defineProperty(this._searchParams, "_url", {
364+
enumerable: false,
365+
writable: false,
366+
value: this,
367+
});
368+
this._searchParams._append = this._searchParams.append;
369+
this._searchParams.append = function (name, value) {
370+
this._append(name, value);
371+
this._url.search = this.toString();
372+
};
373+
this._searchParams._delete = this._searchParams.delete;
374+
this._searchParams.delete = function (name) {
375+
this._delete(name);
376+
this._url.search = this.toString();
377+
};
378+
this._searchParams._set = this._searchParams.set;
379+
this._searchParams.set = function (name, value) {
380+
this._set(name, value);
381+
this._url.search = this.toString();
382+
};
383+
this._searchParams._sort = this._searchParams.sort;
384+
this._searchParams.sort = function () {
385+
this._sort();
386+
this._url.search = this.toString();
387+
};
388+
}
389+
return this._searchParams;
390+
},
391+
});
392+
}
334393
})();

test-app/runtime/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,23 @@ include_directories(
6868
src/main/cpp/runtime/jsonhelper
6969
src/main/cpp/runtime/version
7070
src/main/cpp/runtime/weakref
71+
72+
src/main/cpp/modules
73+
src/main/cpp/modules/url
7174
)
7275

73-
# Search for all CPP files in runtime/ directory and add them to our sourcess
76+
# Search for all CPP files in runtime/ directory and add them to our sources
7477
file(GLOB_RECURSE RUNTIME_FILES
7578
"${PROJECT_SOURCE_DIR}/src/main/cpp/runtime/*.cpp"
7679
"${PROJECT_SOURCE_DIR}/src/main/cpp/runtime/**/*.cpp"
7780
)
7881

79-
set(SOURCES ${RUNTIME_FILES})
82+
file (GLOB_RECURSE MODULE_FILES
83+
"${PROJECT_SOURCE_DIR}/src/main/cpp/modules/*.cpp"
84+
"${PROJECT_SOURCE_DIR}/src/main/cpp/modules/**/*.cpp"
85+
)
86+
87+
set(SOURCES ${RUNTIME_FILES} ${MODULE_FILES})
8088

8189
if (QUICKJS)
8290
add_subdirectory(${PROJECT_SOURCE_DIR}/src/main/cpp/napi/quickjs/mimalloc-dev mimalloc)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by Ammar Ahmed on 01/03/2025.
3+
//
4+
5+
#ifndef TEST_APP_RUNTIMEMODULES_H
6+
#define TEST_APP_RUNTIMEMODULES_H
7+
8+
#include "js_native_api.h"
9+
#include "URL.h"
10+
#include "URLSearchParams.h"
11+
12+
namespace tns {
13+
class NSRuntimeModules {
14+
public:
15+
static void Init(napi_env env) {
16+
URL::Init(env);
17+
URLSearchParams::Init(env);
18+
}
19+
};
20+
}
21+
22+
#endif //TEST_APP_RUNTIMEMODULES_H

0 commit comments

Comments
 (0)