<commit_before>/*
* nodekit.io
*
* Copyright (c) 2016 OffGrid Networks. All Rights Reserved.
* Portions Copyright 2015 XWebView
* Portions Copyright (c) 2014 Intel Corporation.  All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import JavaScriptCore

open class NKJSContext: NSObject {
    
    fileprivate let _jsContext: JSContext
    fileprivate let _id: Int
    
    private var plugins: [String: NKNativePlugin] = [:] //namespace -> plugin
    private var sources: [String: NKScriptSource] = [:] //filename -> source
    
    internal init(_ jsContext: JSContext, id: Int) {
        _jsContext = jsContext
        _id = id
        super.init()
    }
    
    internal func prepareEnvironment() -> Void {
        
        
        let logjs: @convention(block) (String, String, [String: AnyObject] ) -> () = { body, severity, labels in
    
            NKLogging.log(body, level: NKLogging.Level(description: severity), labels: labels);
        }
        
        _jsContext.exceptionHandler =  { (ctx: JSContext?, value: JSValue?) in
            NKLogging.log("JavaScript Error");
            // type of String
            let stacktrace = value?.objectForKeyedSubscript("stack").toString() ?? "No stack trace"
            // type of Number
            let lineNumber: Any = value?.objectForKeyedSubscript("line") ?? "Unknown"
            // type of Number
            let column: Any = value?.objectForKeyedSubscript("column") ?? "Unknown"
            let moreInfo = "in method \(stacktrace) Line number: \(lineNumber), column: \(column)"
            
            let errorString = value.map { $0.description } ?? "null"
            
            NKLogging.log("JavaScript Error: \(errorString) \(moreInfo)")
        }
        
        let scriptingBridge = JSValue(newObjectIn: _jsContext)
        
        scriptingBridge?.setObject(unsafeBitCast(logjs, to: AnyObject.self), forKeyedSubscript: "log" as NSString)
        _jsContext.setObject(unsafeBitCast(scriptingBridge, to: AnyObject.self), forKeyedSubscript: "NKScriptingBridge" as NSString)
        
        let appjs = NKStorage.getResource("lib-scripting.nkar/lib-scripting/init_jsc.js", NKJSContext.self)
        
        let script = "function loadinit(){\n" + appjs! + "\n}\n" + "loadinit();" + "\n"
        
        self.injectJavaScript(NKScriptSource(source: script, asFilename: "io.nodekit.scripting/init_jsc", namespace: "io.nodekit.scripting.init"))
        
        guard let promiseSource = NKStorage.getResource("lib-scripting.nkar/lib-scripting/promise.js", NKJSContext.self) else {
            NKLogging.die("Failed to read provision script: promise")<commit_msg>remove unnecessary unsafeBitCasts<commit_after>/*
* nodekit.io
*
* Copyright (c) 2016 OffGrid Networks. All Rights Reserved.
* Portions Copyright 2015 XWebView
* Portions Copyright (c) 2014 Intel Corporation.  All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import JavaScriptCore

open class NKJSContext: NSObject {
    
    fileprivate let _jsContext: JSContext
    fileprivate let _id: Int
    
    private var plugins: [String: NKNativePlugin] = [:] //namespace -> plugin
    private var sources: [String: NKScriptSource] = [:] //filename -> source
    
    internal init(_ jsContext: JSContext, id: Int) {
        _jsContext = jsContext
        _id = id
        super.init()
    }
    
    internal func prepareEnvironment() -> Void {
        
        
        let logjs: @convention(block) (String, String, [String: AnyObject] ) -> () = { body, severity, labels in
    
            NKLogging.log(body, level: NKLogging.Level(description: severity), labels: labels);
        }
        
        _jsContext.exceptionHandler =  { (ctx: JSContext?, value: JSValue?) in
            NKLogging.log("JavaScript Error");
            // type of String
            let stacktrace = value?.objectForKeyedSubscript("stack").toString() ?? "No stack trace"
            // type of Number
            let lineNumber: Any = value?.objectForKeyedSubscript("line") ?? "Unknown"
            // type of Number
            let column: Any = value?.objectForKeyedSubscript("column") ?? "Unknown"
            let moreInfo = "in method \(stacktrace) Line number: \(lineNumber), column: \(column)"
            
            let errorString = value.map { $0.description } ?? "null"
            
            NKLogging.log("JavaScript Error: \(errorString) \(moreInfo)")
        }
        
        let scriptingBridge = JSValue(newObjectIn: _jsContext)
        
        scriptingBridge?.setObject(logjs as AnyObject, forKeyedSubscript: "log" as NSString)
        _jsContext.setObject(scriptingBridge, forKeyedSubscript: "NKScriptingBridge" as NSString)
        
        let appjs = NKStorage.getResource("lib-scripting.nkar/lib-scripting/init_jsc.js", NKJSContext.self)
        
        let script = "function loadinit(){\n" + appjs! + "\n}\n" + "loadinit();" + "\n"
        
        self.injectJavaScript(NKScriptSource(source: script, asFilename: "io.nodekit.scripting/init_jsc", namespace: "io.nodekit.scripting.init"))
        
        guard let promiseSource = NKStorage.getResource("lib-scripting.nkar/lib-scripting/promise.js", NKJSContext.self) else {
            NKLogging.die("Failed to read provision script: promise")
{'fraction_non_alphanumeric': 0.10685323580855462, 'fraction_numerical': 0.005406264906980442, 'mean_word_length': 2.7914406268836647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5891551', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1712, 'n_words': 521}