Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Examples/ActorOnWebWorker/Sources/MyApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ actor SearchService {

// Utility function for fetch
func fetch(_ url: String) -> JSPromise {
let jsFetch = JSObject.global.fetch.function!
let jsFetch = JSObject.global.fetch.object!
return JSPromise(jsFetch(url).object!)!
}

Expand Down Expand Up @@ -117,7 +117,7 @@ struct SearchResult {
@MainActor
final class App {
private let document = JSObject.global.document
private let alert = JSObject.global.alert.function!
private let alert = JSObject.global.alert.object!

// UI elements
private let container: JSValue
Expand Down
4 changes: 2 additions & 2 deletions Examples/Basic/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import JavaScriptEventLoop
import JavaScriptKit

let alert = JSObject.global.alert.function!
let alert = JSObject.global.alert.object!
let document = JSObject.global.document

let divElement = document.createElement("div")
Expand All @@ -19,7 +19,7 @@ buttonElement.onclick = .object(

_ = document.body.appendChild(buttonElement)

private let jsFetch = JSObject.global.fetch.function!
private let jsFetch = JSObject.global.fetch.object!
func fetch(_ url: String) -> JSPromise {
JSPromise(jsFetch(url).object!)!
}
Expand Down
6 changes: 3 additions & 3 deletions Examples/Embedded/Sources/EmbeddedApp/main.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JavaScriptKit

let alert = JSObject.global.alert.function!
let alert = JSObject.global.alert.object!
let document = JSObject.global.document

print("Hello from WASM, document title: \(document.title.string ?? "")")
Expand Down Expand Up @@ -28,8 +28,8 @@ textInputElement.type = "text"
textInputElement.placeholder = "Enter text to encode to UTF-8"
textInputElement.oninput = JSValue.object(
JSClosure { _ in
let textEncoder = JSObject.global.TextEncoder.function!.new()
let encode = textEncoder.encode.function!
let textEncoder = JSObject.global.TextEncoder.object!.new()
let encode = textEncoder.encode.object!
let encodedData = JSTypedArray<UInt8>(
unsafelyWrapping: encode(this: textEncoder, textInputElement.value).object!
)
Expand Down
3 changes: 1 addition & 2 deletions Plugins/PackageToJS/Templates/runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const decode = (kind, payload1, payload2, objectSpace) => {
return payload2;
case 1 /* Kind.String */:
case 3 /* Kind.Object */:
case 6 /* Kind.Function */:
case 7 /* Kind.Symbol */:
case 8 /* Kind.BigInt */:
return objectSpace.getObject(payload1);
Expand Down Expand Up @@ -100,7 +99,7 @@ const writeAndReturnKindBits = (value, payload1_ptr, payload2_ptr, is_exception,
return writeRef(3 /* Kind.Object */);
}
case "function": {
return writeRef(6 /* Kind.Function */);
return writeRef(3 /* Kind.Object */);
}
case "symbol": {
return writeRef(7 /* Kind.Symbol */);
Expand Down
4 changes: 1 addition & 3 deletions Runtime/src/js-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const enum Kind {
Object = 3,
Null = 4,
Undefined = 5,
Function = 6,
Symbol = 7,
BigInt = 8,
}
Expand All @@ -32,7 +31,6 @@ export const decode = (

case Kind.String:
case Kind.Object:
case Kind.Function:
case Kind.Symbol:
case Kind.BigInt:
return objectSpace.getObject(payload1);
Expand Down Expand Up @@ -129,7 +127,7 @@ export const writeAndReturnKindBits = (
return writeRef(Kind.Object);
}
case "function": {
return writeRef(Kind.Function);
return writeRef(Kind.Object);
}
case "symbol": {
return writeRef(Kind.Symbol);
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptBigIntSupport/Int64+I64.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import JavaScriptKit

extension UInt64: JavaScriptKit.ConvertibleToJSValue, JavaScriptKit.TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.BigUint64Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.BigUint64Array.object! }

public var jsValue: JSValue { .bigInt(JSBigInt(unsigned: self)) }
}

extension Int64: JavaScriptKit.ConvertibleToJSValue, JavaScriptKit.TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.BigInt64Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.BigInt64Array.object! }

public var jsValue: JSValue { .bigInt(JSBigInt(self)) }
}
10 changes: 5 additions & 5 deletions Sources/JavaScriptEventLoop/JSSending.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Synchronization
///
/// ```swift
/// // Transfer an object to another thread
/// let buffer = JSObject.global.Uint8Array.function!.new(100).buffer.object!
/// let buffer = JSObject.global.Uint8Array.object!.new(100).buffer.object!
/// let transferring = JSSending.transfer(buffer)
///
/// // Receive the object on a worker thread
Expand All @@ -34,7 +34,7 @@ import Synchronization
/// }
///
/// // Clone an object for use in another thread
/// let object = JSObject.global.Object.function!.new()
/// let object = JSObject.global.Object.object!.new()
/// object["test"] = "Hello, World!"
/// let cloning = JSSending(object)
///
Expand Down Expand Up @@ -136,7 +136,7 @@ extension JSSending where T == JSObject {
/// ## Example
///
/// ```swift
/// let buffer = JSObject.global.Uint8Array.function!.new(100).buffer.object!
/// let buffer = JSObject.global.Uint8Array.object!.new(100).buffer.object!
/// let transferring = JSSending.transfer(buffer)
///
/// // After transfer, the original buffer is neutered
Expand Down Expand Up @@ -167,7 +167,7 @@ extension JSSending where T == JSObject {
/// ## Example
///
/// ```swift
/// let object = JSObject.global.Object.function!.new()
/// let object = JSObject.global.Object.object!.new()
/// object["test"] = "Hello, World!"
/// let cloning = JSSending(object)
///
Expand Down Expand Up @@ -212,7 +212,7 @@ extension JSSending {
/// ## Example - Cloning
///
/// ```swift
/// let data = JSObject.global.Object.function!.new()
/// let data = JSObject.global.Object.object!.new()
/// data["value"] = 42
/// let cloning = JSSending(data)
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
let promise = JSPromise(resolver: { resolver -> Void in
resolver(.success(.undefined))
})
let setTimeout = JSObject.global.setTimeout.function!
let setTimeout = JSObject.global.setTimeout.object!
let eventLoop = JavaScriptEventLoop(
queueTask: { job in
// TODO(katei): Should prefer `queueMicrotask` if available?
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptEventLoop/WebWorkerTaskExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import WASILibc
///
/// ```swift
/// // Create and transfer an object to a worker thread
/// let buffer = JSObject.global.ArrayBuffer.function!.new(1024).object!
/// let buffer = JSObject.global.ArrayBuffer.object!.new(1024).object!
/// let transferring = JSSending.transfer(buffer)
///
/// let task = Task(executorPreference: executor) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/BasicObjects/JSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/// class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)
/// that exposes its properties in a type-safe and Swifty way.
public class JSArray: JSBridgedClass {
public static var constructor: JSFunction? { _constructor.wrappedValue }
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Array.function })
public static var constructor: JSObject? { _constructor.wrappedValue }
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Array.object })

static func isArray(_ object: JSObject) -> Bool {
constructor!.isArray!(object).boolean!
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/BasicObjects/JSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/// property if you need those.
public final class JSDate: JSBridgedClass {
/// The constructor function used to create new `Date` objects.
public static var constructor: JSFunction? { _constructor.wrappedValue }
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Date.function })
public static var constructor: JSObject? { _constructor.wrappedValue }
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Date.object })

/// The underlying JavaScript `Date` object.
public let jsObject: JSObject
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/BasicObjects/JSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/// exposes its properties in a type-safe way.
public final class JSError: JSBridgedClass {
/// The constructor function used to create new JavaScript `Error` objects.
public static var constructor: JSFunction? { _constructor.wrappedValue }
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Error.function })
public static var constructor: JSObject? { _constructor.wrappedValue }
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Error.object })

/// The underlying JavaScript `Error` object.
public let jsObject: JSObject
Expand Down
8 changes: 4 additions & 4 deletions Sources/JavaScriptKit/BasicObjects/JSPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public final class JSPromise: JSBridgedClass {
.object(jsObject)
}

public static var constructor: JSFunction? {
JSObject.global.Promise.function!
public static var constructor: JSObject? {
JSObject.global.Promise.object!
}

/// This private initializer assumes that the passed object is a JavaScript `Promise`
Expand Down Expand Up @@ -54,8 +54,8 @@ public final class JSPromise: JSBridgedClass {
let closure = JSOneshotClosure { arguments in
// The arguments are always coming from the `Promise` constructor, so we should be
// safe to assume their type here
let resolve = arguments[0].function!
let reject = arguments[1].function!
let resolve = arguments[0].object!
let reject = arguments[1].object!

resolver {
switch $0 {
Expand Down
8 changes: 4 additions & 4 deletions Sources/JavaScriptKit/BasicObjects/JSTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public final class JSTimer {
}
self.isRepeating = isRepeating
if isRepeating {
value = global.setInterval.function!(arguments: [closure.jsValue, millisecondsDelay.jsValue])
value = global.setInterval.object!(arguments: [closure.jsValue, millisecondsDelay.jsValue])
} else {
value = global.setTimeout.function!(arguments: [closure.jsValue, millisecondsDelay.jsValue])
value = global.setTimeout.object!(arguments: [closure.jsValue, millisecondsDelay.jsValue])
}
}

Expand All @@ -87,9 +87,9 @@ public final class JSTimer {
*/
deinit {
if isRepeating {
global.clearInterval.function!(value)
global.clearInterval.object!(value)
} else {
global.clearTimeout.function!(value)
global.clearTimeout.object!(value)
}
closure.release()
}
Expand Down
34 changes: 17 additions & 17 deletions Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import _CJavaScriptKit
public protocol TypedArrayElement {
associatedtype Element: ConvertibleToJSValue, ConstructibleFromJSValue = Self
/// The constructor function for the TypedArray class for this particular kind of number
static var typedArrayClass: JSFunction { get }
static var typedArrayClass: JSObject { get }
}

/// A wrapper around all [JavaScript `TypedArray`
/// classes](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
/// that exposes their properties in a type-safe way.
public final class JSTypedArray<Traits>: JSBridgedClass, ExpressibleByArrayLiteral where Traits: TypedArrayElement {
public typealias Element = Traits.Element
public class var constructor: JSFunction? { Traits.typedArrayClass }
public class var constructor: JSObject? { Traits.typedArrayClass }
public var jsObject: JSObject

public subscript(_ index: Int) -> Element {
Expand Down Expand Up @@ -141,64 +141,64 @@ public final class JSTypedArray<Traits>: JSBridgedClass, ExpressibleByArrayLiter
}

extension Int: TypedArrayElement {
public static var typedArrayClass: JSFunction {
public static var typedArrayClass: JSObject {
#if _pointerBitWidth(_32)
return JSObject.global.Int32Array.function!
return JSObject.global.Int32Array.object!
#elseif _pointerBitWidth(_64)
return JSObject.global.Int64Array.function!
return JSObject.global.Int64Array.object!
#else
#error("Unsupported pointer width")
#endif
}
}

extension UInt: TypedArrayElement {
public static var typedArrayClass: JSFunction {
public static var typedArrayClass: JSObject {
#if _pointerBitWidth(_32)
return JSObject.global.Uint32Array.function!
return JSObject.global.Uint32Array.object!
#elseif _pointerBitWidth(_64)
return JSObject.global.Uint64Array.function!
return JSObject.global.Uint64Array.object!
#else
#error("Unsupported pointer width")
#endif
}
}

extension Int8: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Int8Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Int8Array.object! }
}

extension UInt8: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Uint8Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Uint8Array.object! }
}

extension Int16: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Int16Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Int16Array.object! }
}

extension UInt16: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Uint16Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Uint16Array.object! }
}

extension Int32: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Int32Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Int32Array.object! }
}

extension UInt32: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Uint32Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Uint32Array.object! }
}

extension Float32: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Float32Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Float32Array.object! }
}

extension Float64: TypedArrayElement {
public static var typedArrayClass: JSFunction { JSObject.global.Float64Array.function! }
public static var typedArrayClass: JSObject { JSObject.global.Float64Array.object! }
}

public enum JSUInt8Clamped: TypedArrayElement {
public typealias Element = UInt8
public static var typedArrayClass: JSFunction { JSObject.global.Uint8ClampedArray.function! }
public static var typedArrayClass: JSObject { JSObject.global.Uint8ClampedArray.object! }
}

public typealias JSUInt8ClampedArray = JSTypedArray<JSUInt8Clamped>
Loading