Skip to content

Commit 41cd019

Browse files
committed
Move JSC from JavaScript to JSCSwift target
1 parent 8c399ad commit 41cd019

File tree

12 files changed

+202
-95
lines changed

12 files changed

+202
-95
lines changed

Package.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ let package = Package(
2727
.target(
2828
name: "CJavaScriptCore",
2929
dependencies: []),
30+
.target(
31+
name: "JavaScriptCoreSwift",
32+
dependencies: ["CJavaScriptCore", "JavaScript"]),
3033
.target(
3134
name: "JavaScript",
32-
dependencies: ["CJavaScriptCore"]),
35+
dependencies: []),
3336
.testTarget(
34-
name: "JavaScriptTests",
35-
dependencies: ["Test", "JavaScript"])
37+
name: "JavaScriptCoreTests",
38+
dependencies: ["Test", "JavaScriptCoreSwift"]),
3639
]
3740
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2017 Tris Foundation and the project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License
6+
*
7+
* See LICENSE.txt in the project root for license information
8+
* See CONTRIBUTORS.txt for the list of the project authors
9+
*/
10+
11+
public protocol JSEngine {
12+
associatedtype JSRuntime: JavaScript.JSRuntime
13+
static func createRuntime() throws -> JSRuntime
14+
}
15+
16+
public protocol JSRuntime {
17+
associatedtype JSContext: JavaScript.JSContext
18+
func createContext() -> JSContext
19+
}
20+
21+
public protocol JSContext {
22+
associatedtype JSValue: JavaScript.JSValue
23+
func evaluate(_ script: String) throws -> JSValue
24+
25+
// func createValue(string: String) -> JSValue
26+
// func createValue(number: Int) -> JSValue
27+
// func createValue(number: Double) -> JSValue
28+
29+
// func createFunction()
30+
}
31+
32+
public protocol JSValue {
33+
func toString() throws -> String
34+
35+
var isNull: Bool { get }
36+
var isUndefined: Bool { get }
37+
var isBool: Bool { get }
38+
var isNumber: Bool { get }
39+
var isString: Bool { get }
40+
var isObject: Bool { get }
41+
}
42+
43+
public enum Value {
44+
case undefined
45+
case null
46+
case bool(Bool)
47+
case number(Double)
48+
case string(String)
49+
}

Sources/JavaScript/JSContext+closure.swift renamed to Sources/JavaScriptCoreSwift/JSContext+closure.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,14 @@ import CJavaScriptCore
1414
import JavaScriptCore
1515
#endif
1616

17-
public enum ReturnValue {
18-
case undefined
19-
case null
20-
case bool(Bool)
21-
case number(Double)
22-
case string(String)
23-
}
17+
@_exported import JavaScript
2418

25-
private var functions: [OpaquePointer: ([JSValue]) throws -> ReturnValue] = [:]
19+
private var functions: [OpaquePointer: ([JSValue]) throws -> Value] = [:]
2620

2721
extension JSContext {
2822
public func createFunction(
2923
name: String,
30-
_ body: @escaping ([JSValue]) throws -> ReturnValue) throws
24+
_ body: @escaping ([JSValue]) throws -> Value) throws
3125
{
3226
let function = try createFunction(name: name, callback: wrapper)
3327
functions[function] = body
@@ -48,7 +42,7 @@ extension JSContext {
4842
extension JSContext {
4943
public func createFunction(
5044
name: String,
51-
_ body: @escaping () throws -> ReturnValue) throws
45+
_ body: @escaping () throws -> Value) throws
5246
{
5347
return try createFunction(name: name) { _ in
5448
return try body()
File renamed without changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ import CJavaScriptCore
1414
import JavaScriptCore
1515
#endif
1616

17+
protocol JSValueInitializable {
18+
init(from jsValue: JSValue) throws
19+
}
20+
21+
extension String: JSValueInitializable {
22+
init(from jsValue: JSValue) throws {
23+
self = try jsValue.toString()
24+
}
25+
}
26+
1727
public class JSValue {
1828
let context: JSContextRef
1929
let pointer: JSValueRef
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2017 Tris Foundation and the project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License
6+
*
7+
* See LICENSE.txt in the project root for license information
8+
* See CONTRIBUTORS.txt for the list of the project authors
9+
*/
10+
11+
import Test
12+
@testable import JavaScriptCoreSwift
13+
14+
final class JSValueTests: TestCase {
15+
func testIsUndefined() {
16+
do {
17+
let context = JSContext()
18+
19+
let result = try context.evaluate("undefined")
20+
assertTrue(result.isUndefined)
21+
assertFalse(result.isNull)
22+
assertFalse(result.isBool)
23+
assertFalse(result.isNumber)
24+
assertFalse(result.isString)
25+
assertEqual(try result.toString(), "undefined")
26+
} catch {
27+
fail(String(describing: error))
28+
}
29+
}
30+
31+
func testIsNull() {
32+
do {
33+
let context = JSContext()
34+
let result = try context.evaluate("null")
35+
assertFalse(result.isUndefined)
36+
assertTrue(result.isNull)
37+
assertFalse(result.isBool)
38+
assertFalse(result.isNumber)
39+
assertFalse(result.isString)
40+
assertEqual(try result.toString(), "null")
41+
} catch {
42+
fail(String(describing: error))
43+
}
44+
}
45+
46+
func testIsBool() {
47+
do {
48+
let context = JSContext()
49+
let result = try context.evaluate("true")
50+
assertFalse(result.isUndefined)
51+
assertFalse(result.isNull)
52+
assertTrue(result.isBool)
53+
assertFalse(result.isNumber)
54+
assertFalse(result.isString)
55+
assertEqual(try result.toString(), "true")
56+
assertEqual(result.toBool(), true)
57+
} catch {
58+
fail(String(describing: error))
59+
}
60+
}
61+
62+
func testIsNumber() {
63+
do {
64+
let context = JSContext()
65+
let result = try context.evaluate("3.14")
66+
assertFalse(result.isUndefined)
67+
assertFalse(result.isNull)
68+
assertFalse(result.isBool)
69+
assertTrue(result.isNumber)
70+
assertFalse(result.isString)
71+
assertEqual(try result.toString(), "3.14")
72+
assertEqual(try result.toDouble(), 3.14)
73+
} catch {
74+
fail(String(describing: error))
75+
}
76+
}
77+
78+
func testIsString() {
79+
do {
80+
let context = JSContext()
81+
let result = try context.evaluate("'success'")
82+
assertFalse(result.isUndefined)
83+
assertFalse(result.isNull)
84+
assertFalse(result.isBool)
85+
assertFalse(result.isNumber)
86+
assertTrue(result.isString)
87+
assertEqual(try result.toString(), "success")
88+
} catch {
89+
fail(String(describing: error))
90+
}
91+
}
92+
93+
func testToInt() {
94+
do {
95+
let context = JSContext()
96+
let result = try context.evaluate("40 + 2")
97+
assertEqual(try result.toInt(), 42)
98+
} catch {
99+
fail(String(describing: error))
100+
}
101+
}
102+
103+
func testToString() {
104+
do {
105+
let context = JSContext()
106+
let result = try context.evaluate("40 + 2")
107+
assertEqual(try result.toString(), "42")
108+
} catch {
109+
fail(String(describing: error))
110+
}
111+
}
112+
113+
func testProperty() {
114+
do {
115+
let context = JSContext()
116+
let result = try context.evaluate("""
117+
(function(){
118+
return { property: 'test' }
119+
})()
120+
""")
121+
122+
assertEqual(try result["property"]?.toString(), "test")
123+
} catch {
124+
fail(String(describing: error))
125+
}
126+
}
127+
}

Tests/JavaScriptTests/JavaScriptTests.swift renamed to Tests/JavaScriptCoreTests/JavaScriptTests.swift

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import Test
12-
@testable import JavaScript
12+
@testable import JavaScriptCoreSwift
1313

1414
final class JavaScriptCoreTests: TestCase {
1515
func testEvaluate() {
@@ -20,7 +20,7 @@ final class JavaScriptCoreTests: TestCase {
2020
func testException() {
2121
let context = JSContext()
2222
assertThrowsError(try context.evaluate("x()")) { error in
23-
assertEqual("\(error)", "ReferenceError: Can't find variable: x")
23+
assertEqual("\(error)", "Can't find variable: x")
2424
}
2525
}
2626

@@ -46,57 +46,30 @@ final class JavaScriptCoreTests: TestCase {
4646
}
4747
let undefinedResult = try context.evaluate("testUndefined()")
4848
assertTrue(undefinedResult.isUndefined)
49-
assertFalse(undefinedResult.isNull)
50-
assertFalse(undefinedResult.isBool)
51-
assertFalse(undefinedResult.isNumber)
52-
assertFalse(undefinedResult.isString)
53-
assertEqual(try undefinedResult.toString(), "undefined")
54-
5549

5650
try context.createFunction(name: "testNull") {
5751
return .null
5852
}
5953
let nullResult = try context.evaluate("testNull()")
60-
assertFalse(nullResult.isUndefined)
6154
assertTrue(nullResult.isNull)
62-
assertFalse(nullResult.isBool)
63-
assertFalse(nullResult.isNumber)
64-
assertFalse(nullResult.isString)
65-
assertEqual(try nullResult.toString(), "null")
66-
6755

6856
try context.createFunction(name: "testBool") {
6957
return .bool(true)
7058
}
7159
let boolResult = try context.evaluate("testBool()")
72-
assertFalse(boolResult.isUndefined)
73-
assertFalse(boolResult.isNull)
7460
assertTrue(boolResult.isBool)
75-
assertFalse(boolResult.isNumber)
76-
assertFalse(boolResult.isString)
77-
assertEqual(boolResult.toBool(), true)
7861

7962
try context.createFunction(name: "testNumber") {
8063
return .number(3.14)
8164
}
8265
let numberResult = try context.evaluate("testNumber()")
83-
assertFalse(numberResult.isUndefined)
84-
assertFalse(numberResult.isNull)
85-
assertFalse(numberResult.isBool)
8666
assertTrue(numberResult.isNumber)
87-
assertFalse(numberResult.isString)
88-
assertEqual(try numberResult.toDouble(), 3.14)
8967

9068
try context.createFunction(name: "testString") {
9169
return .string("success")
9270
}
9371
let stringResult = try context.evaluate("testString()")
94-
assertFalse(stringResult.isUndefined)
95-
assertFalse(stringResult.isNull)
96-
assertFalse(stringResult.isBool)
97-
assertFalse(stringResult.isNumber)
9872
assertTrue(stringResult.isString)
99-
assertEqual(try stringResult.toString(), "success")
10073
} catch {
10174
fail(String(describing: error))
10275
}
@@ -108,7 +81,7 @@ final class JavaScriptCoreTests: TestCase {
10881

10982
var captured = false
11083
try context.createFunction(name: "testCapture")
111-
{ (_) -> ReturnValue in
84+
{ (_) -> Value in
11285
captured = true
11386
return .string("captured")
11487
}

Tests/JavaScriptTests/XCTestManifests.swift renamed to Tests/JavaScriptCoreTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import XCTest
22

33
extension JSValueTests {
44
static let __allTests = [
5+
("testProperty", testProperty),
56
("testToInt", testToInt),
67
("testToString", testToString),
78
]

0 commit comments

Comments
 (0)