Skip to content

Commit bc51ac6

Browse files
Optimize _get_subscript to reduce memory store
1 parent 8184119 commit bc51ac6

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

Runtime/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,13 @@ export class SwiftRuntime {
161161
swjs_get_subscript: (
162162
ref: ref,
163163
index: number,
164-
kind_ptr: pointer,
165164
payload1_ptr: pointer,
166165
payload2_ptr: pointer
167166
) => {
168167
const obj = this.memory.getObject(ref);
169168
const result = obj[index];
170-
JSValue.write(
169+
return JSValue.writeV2(
171170
result,
172-
kind_ptr,
173171
payload1_ptr,
174172
payload2_ptr,
175173
false,

Runtime/src/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ export interface ImportedFunctions {
4545
swjs_get_subscript(
4646
ref: number,
4747
index: number,
48-
kind_ptr: pointer,
4948
payload1_ptr: pointer,
5049
payload2_ptr: pointer
51-
): void;
50+
): JavaScriptValueKind;
5251
swjs_encode_string(ref: number, bytes_ptr_result: pointer): number;
5352
swjs_decode_string(bytes_ptr: pointer, length: number): number;
5453
swjs_load_string(ref: number, buffer: pointer): void;

Sources/JavaScriptKit/JSValue.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ public func setJSValue(this: JSObject, name: JSString, value: JSValue) {
212212

213213
public func getJSValue(this: JSObject, index: Int32) -> JSValue {
214214
var rawValue = RawJSValue()
215-
_get_subscript(this.id, index,
216-
&rawValue.kind,
217-
&rawValue.payload1, &rawValue.payload2)
215+
let rawBitPattern = _get_subscript(
216+
this.id, index,
217+
&rawValue.payload1, &rawValue.payload2
218+
)
219+
rawValue.kind = unsafeBitCast(rawBitPattern, to: JavaScriptValueKind.self)
218220
return rawValue.jsValue
219221
}
220222

Sources/JavaScriptKit/XcodeSupport.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ import _CJavaScriptKit
2929
func _get_subscript(
3030
_: JavaScriptObjectRef,
3131
_: Int32,
32-
_: UnsafeMutablePointer<JavaScriptValueKind>!,
3332
_: UnsafeMutablePointer<JavaScriptPayload1>!,
3433
_: UnsafeMutablePointer<JavaScriptPayload2>!
35-
) { fatalError() }
34+
) -> UInt32 { fatalError() }
3635
func _encode_string(
3736
_: JavaScriptObjectRef,
3837
_: UnsafeMutablePointer<JavaScriptObjectRef>!

Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,17 @@ extern void _set_subscript(const JavaScriptObjectRef _this,
124124
///
125125
/// @param _this The target JavaScript object to get its member value.
126126
/// @param index A subscript index to get value.
127-
/// @param kind A result pointer of JavaScript value kind to get.
128127
/// @param payload1 A result pointer of first payload of JavaScript value to get the target object.
129128
/// @param payload2 A result pointer of second payload of JavaScript value to get the target object.
129+
/// @return A JavaScriptValueKind bits represented as 32bit integer for the returned value.
130130
__attribute__((__import_module__("javascript_kit"),
131131
__import_name__("swjs_get_subscript")))
132-
extern void _get_subscript(const JavaScriptObjectRef _this,
133-
const int index,
134-
JavaScriptValueKind *kind,
135-
JavaScriptPayload1 *payload1,
136-
JavaScriptPayload2 *payload2);
132+
extern uint32_t _get_subscript(
133+
const JavaScriptObjectRef _this,
134+
const int index,
135+
JavaScriptPayload1 *payload1,
136+
JavaScriptPayload2 *payload2
137+
);
137138

138139
/// `_encode_string` encodes the `str_obj` to bytes sequence and returns the length of bytes.
139140
///

0 commit comments

Comments
 (0)