|
| 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 | +#if os(Linux) |
| 12 | +import CJavaScriptCore |
| 13 | +#else |
| 14 | +import JavaScriptCore |
| 15 | +#endif |
| 16 | + |
| 17 | +public enum ReturnValue { |
| 18 | + case undefined |
| 19 | + case null |
| 20 | + case bool(Bool) |
| 21 | + case number(Double) |
| 22 | + case string(String) |
| 23 | +} |
| 24 | + |
| 25 | +private var functions: [OpaquePointer: ([JSValue]) throws -> ReturnValue] = [:] |
| 26 | + |
| 27 | +extension JSContext { |
| 28 | + public func createFunction( |
| 29 | + name: String, |
| 30 | + _ body: @escaping ([JSValue]) throws -> ReturnValue) throws |
| 31 | + { |
| 32 | + let function = try createFunction(name: name, callback: wrapper) |
| 33 | + functions[function] = body |
| 34 | + } |
| 35 | + |
| 36 | + public func createFunction( |
| 37 | + name: String, |
| 38 | + _ body: @escaping ([JSValue]) throws -> Void) throws |
| 39 | + { |
| 40 | + let function = try createFunction(name: name, callback: wrapper) |
| 41 | + functions[function] = { arguments in |
| 42 | + try body(arguments) |
| 43 | + return .undefined |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +extension JSContext { |
| 49 | + public func createFunction( |
| 50 | + name: String, |
| 51 | + _ body: @escaping () throws -> ReturnValue) throws |
| 52 | + { |
| 53 | + return try createFunction(name: name) { _ in |
| 54 | + return try body() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public func createFunction( |
| 59 | + name: String, |
| 60 | + _ body: @escaping () throws -> Void) throws |
| 61 | + { |
| 62 | + try createFunction(name: name) { _ in |
| 63 | + try body() |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func wrapper( |
| 69 | + ctx: JSContextRef!, |
| 70 | + function: JSObjectRef!, |
| 71 | + thisObject: JSObjectRef!, |
| 72 | + argumentCount: Int, |
| 73 | + arguments: UnsafePointer<JSValueRef?>?, |
| 74 | + exception: UnsafeMutablePointer<JSValueRef?>?) -> JSValueRef? |
| 75 | +{ |
| 76 | + guard let body = functions[function] else { |
| 77 | + if let exception = exception { |
| 78 | + let error = "swift error: unregistered function" |
| 79 | + exception.pointee = JSValue(string: error, in: ctx).pointer |
| 80 | + } |
| 81 | + return nil |
| 82 | + } |
| 83 | + do { |
| 84 | + let arguments = [JSValue]( |
| 85 | + start: arguments, |
| 86 | + count: argumentCount, |
| 87 | + in: ctx) |
| 88 | + let result = try body(arguments) |
| 89 | + switch result { |
| 90 | + case .undefined: return JSValueMakeUndefined(ctx) |
| 91 | + case .null: return JSValueMakeNull(ctx) |
| 92 | + case .bool(let value): return JSValueMakeBoolean(ctx, value) |
| 93 | + case .number(let value): return JSValueMakeNumber(ctx, value) |
| 94 | + case .string(let value): return JSValue(string: value, in: ctx).pointer |
| 95 | + } |
| 96 | + } catch { |
| 97 | + if let exception = exception { |
| 98 | + exception.pointee = JSValue(string: "\(error)", in: ctx).pointer |
| 99 | + } |
| 100 | + return nil |
| 101 | + } |
| 102 | +} |
0 commit comments