Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Vanilla JavaScript はマルチプラットフォームの夢を見るか

Vanilla JavaScript はマルチプラットフォームの夢を見るか

Mitsuharu Emoto

May 17, 2024
Tweet

More Decks by Mitsuharu Emoto

Other Decks in Programming

Transcript

  1. +4ؔ਺ͷ࣮ߦ w +4$POUFYUͰKTϑΝ ΠϧΛಡΈࠐΉ w ؔ਺໊Λࢦఆͯ͠ɺؔ ਺Λऔಘ࣮ͯ͠ߦ͢Δ w ؔ਺͓Αͼ໭Γ஋͸ +47BMVFͱ͍͏ܕͰ

    ѻ͏ import JavaScriptCore guard let path = Bundle.main.path(forResource: "bundle", ofType: "js"), let contents = try? String(contentsOfFile: path) else { return } let context: JSContext = JSContext(virtualMachine: JSVirtualMachine()) context.evaluateScript(contents) guard let function = context.objectForKeyedSubscript(name: "func_name") else { return } let arguments = ؔ਺ͷҾ਺ʢจࣈɺ਺ࣈɺ഑ྻͳͲ [Any] ͷܕʣ let result = function.call(withArguments: [arguments]) return result 5
  2. ΤϥʔϋϯυϦϯά w +4ϑΝΠϧͷಡΈࠐΈ΍ؔ਺࣮ߦͷΤϥʔ͸εϩʔ͞Εͳ͍ w +4$POUFYUͷΤϥʔϋϯυϦϯάؔ਺ΛཔΓʹݪҼΛ௥ٻͯ͠मਖ਼͢Δ w ஍ຯ͕ͩɺͱͯ΋ॏཁͳϝιουͰ͢ context.exceptionHandler = {

    context, error in guard let error = error, let message = error.toString() else { return } print("JSContext#exceptionHandler Error: \(message)") } 6
  3. 8FCQBDLͰ̍ͭͷϑΝΠϧʹ w ෳ਺ͷϑΝΠϧΛ̍ͭͷϑΝΠϧʹ·ͱΊͯ͘Ε ΔϞδϡʔϧόϯυϥ w ྫ͑͹ɺ#SJEHFͱͳΔJOEFYKTΛ࡞੒ͯ͠ɺͦ ΕΛର৅ʹͯ͠CVOEMFϑΝΠϧΛ࡞੒͢Δ w ઃఆϑΝΠϧXFCQBDLDPO fi

    HKTͳͲॻ͖ํ͸ল ུʢ෇࿥ͷ63-Λݟ͍ͯͩ͘͞ʣ yarn add -D webpack wepback-cli yarn webpack // index.js import hoge from "hoge" import piyo from "piyo" export class Bridge { static hoge(arg) { return hoge(arg) } static piyo(arg) { return piyo(arg) } } 8
  4. Ϟδϡʔϧ࡞੒ޙͷಡΈࠐΈ w όϯυϧϑΝΠϧ͕ϞδϡʔϧԽ͞ΕͨͷͰɺؔ਺औಘ·Ͱͷεςοϓ͕ ૿͕͑ͨɺ͜Ε·Ͱ࢖͍ํͱ͸มΘΒͳ͍ guard let module = context.objectForKeyedSubscript("module_name"), let

    bridge = module.objectForKeyedSubscript("bridge_name"), let function = bridge.objectForKeyedSubscript("function_name") else { return } let arguments = ؔ਺ͷҾ਺ʢจࣈɺ਺ࣈɺ഑ྻͳͲ [Any] ͷܕʣ let result = function.call(withArguments: [arguments]) return result 9