Skip to content

Commit 8184119

Browse files
Optimize swjs_get_prop to reduce memory store
1 parent 5a3ecf2 commit 8184119

File tree

7 files changed

+25
-23
lines changed

7 files changed

+25
-23
lines changed

Runtime/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,15 @@ export class SwiftRuntime {
130130
swjs_get_prop: (
131131
ref: ref,
132132
name: ref,
133-
kind_ptr: pointer,
134133
payload1_ptr: pointer,
135134
payload2_ptr: pointer
136135
) => {
137136
const memory = this.memory;
138137
const obj = memory.getObject(ref);
139138
const key = memory.getObject(name);
140139
const result = obj[key];
141-
JSValue.write(
140+
return JSValue.writeV2(
142141
result,
143-
kind_ptr,
144142
payload1_ptr,
145143
payload2_ptr,
146144
false,

Runtime/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as JSValue from "./js-value.js";
33
export type ref = number;
44
export type pointer = number;
55
export type bool = number;
6+
export type JavaScriptValueKind = number;
67
export type JavaScriptValueKindAndFlags = number;
78

89
export interface ExportedFunctions {
@@ -31,10 +32,9 @@ export interface ImportedFunctions {
3132
swjs_get_prop(
3233
ref: number,
3334
name: number,
34-
kind_ptr: pointer,
3535
payload1_ptr: pointer,
3636
payload2_ptr: pointer
37-
): void;
37+
): JavaScriptValueKind;
3838
swjs_set_subscript(
3939
ref: number,
4040
index: number,

Sources/JavaScriptKit/JSValue.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ extension JSValue: ExpressibleByNilLiteral {
196196

197197
public func getJSValue(this: JSObject, name: JSString) -> JSValue {
198198
var rawValue = RawJSValue()
199-
_get_prop(this.id, name.asInternalJSRef(),
200-
&rawValue.kind,
201-
&rawValue.payload1, &rawValue.payload2)
199+
let rawBitPattern = _get_prop(
200+
this.id, name.asInternalJSRef(),
201+
&rawValue.payload1, &rawValue.payload2
202+
)
203+
rawValue.kind = unsafeBitCast(rawBitPattern, to: JavaScriptValueKind.self)
202204
return rawValue.jsValue
203205
}
204206

@@ -226,9 +228,11 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
226228

227229
public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
228230
var rawValue = RawJSValue()
229-
_get_prop(this.id, symbol.id,
230-
&rawValue.kind,
231-
&rawValue.payload1, &rawValue.payload2)
231+
let rawBitPattern = _get_prop(
232+
this.id, symbol.id,
233+
&rawValue.payload1, &rawValue.payload2
234+
)
235+
rawValue.kind = unsafeBitCast(rawBitPattern, to: JavaScriptValueKind.self)
232236
return rawValue.jsValue
233237
}
234238

Sources/JavaScriptKit/Runtime/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/JavaScriptKit/Runtime/index.mjs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/JavaScriptKit/XcodeSupport.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import _CJavaScriptKit
1616
func _get_prop(
1717
_: JavaScriptObjectRef,
1818
_: JavaScriptObjectRef,
19-
_: UnsafeMutablePointer<JavaScriptValueKind>!,
2019
_: UnsafeMutablePointer<JavaScriptPayload1>!,
2120
_: UnsafeMutablePointer<JavaScriptPayload2>!
22-
) { fatalError() }
21+
) -> UInt32 { fatalError() }
2322
func _set_subscript(
2423
_: JavaScriptObjectRef,
2524
_: Int32,

Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,17 @@ extern void _set_prop(const JavaScriptObjectRef _this,
9393
///
9494
/// @param _this The target JavaScript object to get its member value.
9595
/// @param prop A JavaScript string object to reference a member of `_this` object.
96-
/// @param kind A result pointer of JavaScript value kind to get.
9796
/// @param payload1 A result pointer of first payload of JavaScript value to set the target object.
9897
/// @param payload2 A result pointer of second payload of JavaScript value to set the target object.
98+
/// @return A JavaScriptValueKind bits represented as 32bit integer for the returned value.
9999
__attribute__((__import_module__("javascript_kit"),
100100
__import_name__("swjs_get_prop")))
101-
extern void _get_prop(const JavaScriptObjectRef _this,
102-
const JavaScriptObjectRef prop,
103-
JavaScriptValueKind *kind,
104-
JavaScriptPayload1 *payload1,
105-
JavaScriptPayload2 *payload2);
101+
extern uint32_t _get_prop(
102+
const JavaScriptObjectRef _this,
103+
const JavaScriptObjectRef prop,
104+
JavaScriptPayload1 *payload1,
105+
JavaScriptPayload2 *payload2
106+
);
106107

107108
/// `_set_subscript` sets a value of `_this` JavaScript object.
108109
///

0 commit comments

Comments
 (0)