|
| 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 CJavaScriptCore |
| 12 | +import struct Foundation.URL |
| 13 | + |
| 14 | +public class JSContext { |
| 15 | + let group: JSContextGroupRef |
| 16 | + let context: JSGlobalContextRef |
| 17 | + var exception: JSObjectRef? = nil |
| 18 | + |
| 19 | + var global: JSObjectRef { |
| 20 | + return JSContextGetGlobalObject(context)! |
| 21 | + } |
| 22 | + |
| 23 | + public init() { |
| 24 | + guard let group = JSContextGroupCreate(), |
| 25 | + let context = JSGlobalContextCreateInGroup(group, nil) else { |
| 26 | + fatalError("can't create context") |
| 27 | + } |
| 28 | + |
| 29 | + self.group = group |
| 30 | + self.context = context |
| 31 | + } |
| 32 | + |
| 33 | + deinit { |
| 34 | + JSGlobalContextRelease(context) |
| 35 | + JSContextGroupRelease(group) |
| 36 | + } |
| 37 | + |
| 38 | + @discardableResult |
| 39 | + public func evaluate( |
| 40 | + _ script: String, |
| 41 | + source: String? = nil |
| 42 | + ) throws -> JSValue { |
| 43 | + let file = JSStringCreateWithUTF8CString(source) |
| 44 | + let script = JSStringCreateWithUTF8CString(script) |
| 45 | + defer { |
| 46 | + JSStringRelease(file) |
| 47 | + JSStringRelease(script) |
| 48 | + } |
| 49 | + let result = try JSEvaluateScript(context, script, global, file, 0) |
| 50 | + return JSValue(context: context, pointer: result) |
| 51 | + } |
| 52 | + |
| 53 | + @discardableResult |
| 54 | + public func createFunction( |
| 55 | + name: String, |
| 56 | + callback: @escaping JSObjectCallAsFunctionCallback |
| 57 | + ) throws -> JSObjectRef { |
| 58 | + let name = JSStringCreateWithUTF8CString(name) |
| 59 | + defer { JSStringRelease(name) } |
| 60 | + let function = JSObjectMakeFunctionWithCallback(context, name, callback) |
| 61 | + try JSObjectSetProperty(context, global, name, function, .none) |
| 62 | + return function! |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: register swift closure as javascript function |
| 67 | + |
| 68 | +public enum ReturnValue { |
| 69 | + case undefined |
| 70 | + case null |
| 71 | + case bool(Bool) |
| 72 | + case number(Double) |
| 73 | + case string(String) |
| 74 | +} |
| 75 | + |
| 76 | +var functions: [OpaquePointer: [OpaquePointer: () throws -> ReturnValue]] = [:] |
| 77 | + |
| 78 | +extension JSContext { |
| 79 | + public func createFunction( |
| 80 | + name: String, |
| 81 | + _ body: @escaping () throws -> ReturnValue |
| 82 | + ) throws { |
| 83 | + let function = try createFunction(name: name, callback: wrapper) |
| 84 | + functions[global, default: [:]][function] = body |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func wrapper( |
| 89 | + ctx: JSContextRef!, |
| 90 | + function: JSObjectRef!, |
| 91 | + thisObject: JSObjectRef!, |
| 92 | + argumentCount: Int, |
| 93 | + arguments: UnsafePointer<JSValueRef?>?, |
| 94 | + exception: UnsafeMutablePointer<JSValueRef?>? |
| 95 | +) -> JSValueRef? { |
| 96 | + guard let body = functions[thisObject]?[function] else { |
| 97 | + if let exception = exception { |
| 98 | + let error = "swift error: unregistered function" |
| 99 | + exception.pointee = JSValue(string: error, in: thisObject).pointer |
| 100 | + } |
| 101 | + return nil |
| 102 | + } |
| 103 | + do { |
| 104 | + let result = try body() |
| 105 | + switch result { |
| 106 | + case .undefined: return JSValueMakeUndefined(ctx) |
| 107 | + case .null: return JSValueMakeNull(ctx) |
| 108 | + case .bool(let value): return JSValueMakeBoolean(ctx, value) |
| 109 | + case .number(let value): return JSValueMakeNumber(ctx, value) |
| 110 | + case .string(let value): return JSValue(string: value, in: ctx).pointer |
| 111 | + } |
| 112 | + } catch { |
| 113 | + if let exception = exception { |
| 114 | + exception.pointee = JSValue(string: "\(error)", in: ctx).pointer |
| 115 | + } |
| 116 | + return nil |
| 117 | + } |
| 118 | +} |
0 commit comments