美文网首页
2019-03-08 React Native JSI 和 JS

2019-03-08 React Native JSI 和 JS

作者: KingAmo | 来源:发表于2019-03-08 14:41 被阅读0次

JSI 是什么

RN 中, JSIJavaScript Interface 的缩写,JSI 是一个轻量级的通用的API框架,可以应用于任意的JavaScript virtual machine,让各种平台可以方便的使用不同的JavaScript解析引擎(JavaScript virtual machine 包含 JavaScript Engine)。

JSI是用C++写的,用于取代原先的bridge,提高通信效率,已在RN 的 0.58 中实现。

目前在RN中,默认使用的JavaScript virtual machineJavaScriptCore (JSC---WbKit中用的就是这个)

有了JSI ,我们就能轻松的直接调用原生UI ViewsNative ModulesJava/ObjC 实现的方法(类似RPC),而不是像原来那样用一层bridge 来排队等待原生层返回的消息

JSC

React Native doesn't compile your JS code to "native" code, instead the JS code is executed by a JavaScript VM supplied either by the OS (JavaScriptCore on iOS) or bundled with the app (for Android: a bundled version of JavaScriptCore, or Facebook's Hermes engine).

JS 代码并不是被编译成 native代码,而是在各个平台的javascript VM中被执行的。在iOS中,javascript VMiOS系统提供的JavaScriptCore;而在Android中,是RN这个框架提供的,打包进APK中的的某个版本的JavaScriptCore(或者是Facebook的Hermes引擎)


  • Fabric: just the UI layer re-architecture, to take full advantage of concurrent React architecture.

  • TurboModules: re-architecture of NativeModules, also using JSI.

  • CodeGen: a tool to "glue" JS and native side: it will allow for supercharge static types (TS, Flow) for compatibilty with native binaries, among other things

The JSI system can also be used to call leverage device capabilities like bluetooth or other sensors by exposing functions that JS can call. This is similar to how browsers expose functions like navigator.geolocation.getCurrentPosition that, when invoked in JavaScript, trigger the respective C++ call in the browser.
In the current system, a table with information about module names and methods is created. When JS calls a specific native module, the indices of the module and methods are passed to Java/ObjC, which then invoke the specific methods. The arguments and return values are also converted between JavaScript and JNI/ObjC objects.
[...] Now that we have a JSI object for "SampleTurboModule", can invoke methods on this JSI object from JavaScript. During the calls, we also need to convert JSI Values to JNI for argument parameters, and the reverse when sending back results.
Like in the current architecture, most types including boolean, strings, Maps, Arrays, Callbacks and Promises are supported

参考链接:

相关文章

网友评论

      本文标题:2019-03-08 React Native JSI 和 JS

      本文链接:https://www.haomeiwen.com/subject/xfsrpqtx.html