Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSBridgedType and JSBridgedClass #26

Merged
merged 26 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
withCompatibleObject → unsafelyWrapping
  • Loading branch information
j-f1 committed Sep 17, 2020
commit ff1f4ac73f71f3229de388639638d57f0b6466fa
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/BasicObjects/JSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class JSArray: JSBridgedClass {
/// - Parameter object: A `JSObject` expected to be a JavaScript Array
public convenience init?(_ jsObject: JSObject) {
guard Self.isArray(jsObject) else { return nil }
self.init(withCompatibleObject: jsObject)
self.init(unsafelyWrapping: jsObject)
}

public required init(withCompatibleObject jsObject: JSObject) {
public required init(unsafelyWrapping jsObject: JSObject) {
self.jsObject = jsObject
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/BasicObjects/JSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class JSDate: JSBridgedClass {
jsObject = Self.constructor.new(year, monthIndex, day, hours, minutes, seconds, milliseconds)
}

public init(withCompatibleObject jsObject: JSObject) {
public init(unsafelyWrapping jsObject: JSObject) {
self.jsObject = jsObject
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/BasicObjects/JSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class JSError: Error, JSBridgedClass {
jsObject = Self.constructor.new([message])
}

public init(withCompatibleObject jsObject: JSObject) {
public init(unsafelyWrapping jsObject: JSObject) {
self.jsObject = jsObject
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
jsObject = Element.typedArrayClass.new(length)
}

required public init(withCompatibleObject jsObject: JSObject) {
required public init(unsafelyWrapping jsObject: JSObject) {
self.jsObject = jsObject
}

Expand All @@ -48,7 +48,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
array.withUnsafeBufferPointer { ptr in
_create_typed_array(Element.typedArrayClass.id, ptr.baseAddress!, Int32(array.count), &resultObj)
}
self.init(withCompatibleObject: JSObject(id: resultObj))
self.init(unsafelyWrapping: JSObject(id: resultObj))
}

/// Convenience initializer for `Sequence`.
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/JSBridgedType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public protocol JSBridgedClass: JSBridgedType {

/// Create an instannce wrapping the given JavaScript object.
/// You may assume that `jsObject instanceof Self.constructor`
init(withCompatibleObject jsObject: JSObject)
init(unsafelyWrapping jsObject: JSObject)
}

extension JSBridgedClass {
public var value: JSValue { jsObject.jsValue() }
public init?(from value: JSValue) {
guard let object = value.object, object.isInstanceOf(Self.constructor) else { return nil }
self.init(withCompatibleObject: object)
self.init(unsafelyWrapping: object)
}
}