Skip to content

Commit bd81b28

Browse files
Start migrating imported functions to the new definition style
Use of different function names for C-name and Wasm's import name is a bit confusing. Also use of unprefixed function names in the C code is not a good practice. This commit starts migrating imported functions to the new naming convention and removes duplicated function definitions just for IDE support by using macro.
1 parent 3a81371 commit bd81b28

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

Sources/JavaScriptKit/JSValue.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public func getJSValue(this: JSObject, name: JSString) -> JSValue {
206206

207207
public func setJSValue(this: JSObject, name: JSString, value: JSValue) {
208208
value.withRawJSValue { rawValue in
209-
_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2)
209+
swjs_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2)
210210
}
211211
}
212212

@@ -240,7 +240,7 @@ public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
240240

241241
public func setJSValue(this: JSObject, symbol: JSSymbol, value: JSValue) {
242242
value.withRawJSValue { rawValue in
243-
_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
243+
swjs_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
244244
}
245245
}
246246

Sources/JavaScriptKit/XcodeSupport.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import _CJavaScriptKit
66
/// to be built for host platforms like macOS or Linux for tentative IDE support.
77
/// (ideally, IDE should build for WebAssembly target though)
88
#if !arch(wasm32)
9-
func _set_prop(
10-
_: JavaScriptObjectRef,
11-
_: JavaScriptObjectRef,
12-
_: JavaScriptValueKind,
13-
_: JavaScriptPayload1,
14-
_: JavaScriptPayload2
15-
) { fatalError() }
169
func _get_prop(
1710
_: JavaScriptObjectRef,
1811
_: JavaScriptObjectRef,

Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,29 @@ typedef struct {
7373
} RawJSValue;
7474

7575
#if __wasm32__
76+
# define IMPORT_JS_FUNCTION(name, returns, args) \
77+
__attribute__((__import_module__("javascript_kit"), __import_name__(#name))) extern returns name args;
78+
#else
79+
# define IMPORT_JS_FUNCTION(name, returns, args) \
80+
static inline returns name args { \
81+
abort(); \
82+
}
83+
#endif
7684

77-
/// `_set_prop` sets a value of `_this` JavaScript object.
85+
/// Set a value of `_this` JavaScript object.
7886
///
7987
/// @param _this The target JavaScript object to set the given value.
8088
/// @param prop A JavaScript string object to reference a member of `_this` object.
8189
/// @param kind A kind of JavaScript value to set the target object.
8290
/// @param payload1 The first payload of JavaScript value to set the target object.
8391
/// @param payload2 The second payload of JavaScript value to set the target object.
84-
__attribute__((__import_module__("javascript_kit"),
85-
__import_name__("swjs_set_prop")))
86-
extern void _set_prop(const JavaScriptObjectRef _this,
87-
const JavaScriptObjectRef prop,
88-
const JavaScriptValueKind kind,
89-
const JavaScriptPayload1 payload1,
90-
const JavaScriptPayload2 payload2);
92+
IMPORT_JS_FUNCTION(swjs_set_prop, void, (const JavaScriptObjectRef _this,
93+
const JavaScriptObjectRef prop,
94+
const JavaScriptValueKind kind,
95+
const JavaScriptPayload1 payload1,
96+
const JavaScriptPayload2 payload2))
97+
98+
#if __wasm32__
9199

92100
/// `_get_prop` gets a value of `_this` JavaScript object.
93101
///

0 commit comments

Comments
 (0)