|
| 1 | +| Title | ABI Stable Module API | |
| 2 | +|--------|---------------------------------------| |
| 3 | +| Author | @mhdawson, @stefanbu, @Ianwjhalliday | |
| 4 | +| Status | DRAFT | |
| 5 | +| Date | 2016-12-12 | |
| 6 | + |
| 7 | +# Introduction |
| 8 | + |
| 9 | +The module ecosystem is one of the features making Node.js the fastest |
| 10 | +growing runtime. An important subset of modules are those which have a |
| 11 | +native component. In specific cases it is important to be able to leverage native |
| 12 | +code in order to be able to: |
| 13 | + |
| 14 | +* use existing third party components written in C/C++ |
| 15 | +* access physical devices, for example a serial port |
| 16 | +* expose functionality from the operating system not otherwise available |
| 17 | +* achieve higher speed than possible in native JavaScript |
| 18 | + |
| 19 | +There is an existing interface (addons) that allows you to build these modules |
| 20 | +as a dynamically-linked shared module that can be loaded into Node.js and |
| 21 | +used as an ordinary Node.js module (for more details see |
| 22 | +https://nodejs.org/api/addons.html) |
| 23 | + |
| 24 | +Unfortunately in the existing implementation the V8 |
| 25 | +(the JavaScript runtime for Node.js) APIs are exposed to modules. In |
| 26 | +addition Google changes these APIs on a regular basis. This results in |
| 27 | +the following issues: |
| 28 | + |
| 29 | +* Native modules must be recompiled for each version of Node.js |
| 30 | +* The code within modules may need to be modified for a new version of |
| 31 | + Node.js |
| 32 | +* It's not clear which parts of the V8 API the Node.js community believes |
| 33 | + are safe/unsafe to use in terms of long term support for that use. |
| 34 | +* Modules written against the V8 APIs may or may not be able to work |
| 35 | + with alternate JavaScript engines when/if Node.js supports them. |
| 36 | + |
| 37 | +The Native Abstractions for Node (NAN) |
| 38 | +project (https://github.com/nodejs/nan) can be used to limit |
| 39 | +the cases when modules must be updated in order to work with new versions |
| 40 | +of Node.js but does not address the other issues as it does not |
| 41 | +encapsulate the V8 argument types such that modules still need |
| 42 | +to be recompiled for new Node.js versions. |
| 43 | + |
| 44 | +# Proposed Action |
| 45 | + |
| 46 | +The goal of this eps is to define an API that: |
| 47 | + |
| 48 | +* Provides a stable API which is not tied to the current |
| 49 | + or further APIs provided by V8 |
| 50 | +* Allows native modules built as shared libraries to be used |
| 51 | + with different versions of Node.js |
| 52 | +* Makes it clear which APIs can use with the expectation |
| 53 | + of not being affected by changes in the JavaScript engine |
| 54 | + or Node.js release |
| 55 | +* The API provided by the Node.js runtime will be in C. In |
| 56 | + addition a C++ wrapper which only contains inline functions |
| 57 | + and uses the C APIs so that the changes in the C++ wrapper |
| 58 | + would not require recompilation. |
| 59 | + |
| 60 | +# Background |
| 61 | + |
| 62 | +There have been ongoing discussions on a new module API in both the |
| 63 | +community api working group (https://github.com/nodejs/api), other |
| 64 | +issues in the Node.js issue tracker, and the recent community Node.js |
| 65 | +face-to-face VM summit (https://github.com/jasnell/vm-summit). |
| 66 | + |
| 67 | +In that summit there was agreement that we wanted to define this |
| 68 | +module API. The general approach was to be: |
| 69 | + |
| 70 | +* Start with NAN, trim down to minimal subset based on module usage |
| 71 | + and inspection. |
| 72 | +* Add in VM agnostic methods for object lifecycle management. |
| 73 | +* Add in VM agnostic exception management |
| 74 | +* Define C/C++ types for the API which are independent from V8 |
| 75 | + (NAN currently uses the V8 types). |
| 76 | +* Use the NAN examples to work through the API and build up the |
| 77 | + definition of the key methods. |
| 78 | +* Validate on most used modules in the ecosystem |
| 79 | +* Complete performance analysis to confirm accepted level of overhead. |
| 80 | + |
| 81 | +Notes/details on other related work and the current experimental progress |
| 82 | +on implementing this eps can be found in: |
| 83 | +https://docs.google.com/document/d/1bX9SZKufbfhr7ncXL9fFev2LrljymtFZdWpwmv7mB9g/edit#heading=h.ln9aj9qjo969 |
| 84 | + |
| 85 | +# API definition |
| 86 | + |
| 87 | +**WORK IN PROGRESS, DRIVEN BY MODULES PORTED SO FAR** |
| 88 | + |
| 89 | +This API will be built up from a minimal set. Based on this set |
| 90 | +we have been able to port the NaN example and level down. Additions |
| 91 | +will be made as we port additional modules. |
| 92 | + |
| 93 | +There has been enough progress that instead of listing the API methods |
| 94 | +here we will point to the header files in the working PoC repos so it |
| 95 | +remains up to date: |
| 96 | + |
| 97 | +Core methods: |
| 98 | +* [node_jsvmapi_types.h](https://github.com/nodejs/abi-stable-node/blob/api-prototype-6.2.0/src/node_jsvmapi_types.h) |
| 99 | +* [node_jsvmapi.h](https://github.com/nodejs/abi-stable-node/blob/api-prototype-6.2.0/src/node_jsvmapi.h) |
| 100 | + |
| 101 | +AsyncWorker support methods: |
| 102 | + |
| 103 | +* [node_asyncapi_types.h](https://github.com/nodejs/abi-stable-node/blob/api-prototype-6.2.0/src/node_asyncapi_types.h) |
| 104 | +* [node_asyncapi.h](https://github.com/nodejs/abi-stable-node/blob/api-prototype-6.2.0/src/node_asyncapi.h) |
| 105 | + |
| 106 | + |
| 107 | +# C++ Wrapper API |
| 108 | + |
| 109 | +The current view is that the API exported by the Node.js binary |
| 110 | +must be in C in order to achieve ABI stability. This [document] |
| 111 | +(http://www.oracle.com/technetwork/articles/servers-storage-dev/stablecplusplusabi-333927.html) |
| 112 | +outlines some of the challenges with respect to C++ and |
| 113 | +ABI stability. |
| 114 | + |
| 115 | +However, we also believe that a C++ wrapper is possible and |
| 116 | +would provide ease of use. The key element is that the C++ part must all |
| 117 | +be inlined and only use the external functions/types exported |
| 118 | +by the C API. |
| 119 | + |
| 120 | +The current set of helpers to provide ease of use are defined in: |
| 121 | + |
| 122 | +[node_api_helpers.h](https://github.com/nodejs/abi-stable-node/blob/api-prototype-6.2.0/src/node_api_helpers.h) |
| 123 | + |
| 124 | +Some of the more complex parts like AsyncWorker will quite likely end up outside |
| 125 | +the scope of the ABI stable API itself, and be in a separate component. |
| 126 | +For example, they may end up part of a version of NaN that supports the new API. |
| 127 | + |
0 commit comments