diff --git a/.eslintignore b/.eslintignore index 195691c9..4dab6e9c 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,7 +1,9 @@ -/dist +**/dist node_modules /lib/typings .eslintrc.js /example /test/.cgenbuild /test/CMakeFiles +/packages/runtime/lib +/packages/emnapi/lib diff --git a/.gitignore b/.gitignore index 6671bc0e..ca02c7b6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ /test/cmake_install.cmake /test/Makefile /test/CMakefiles -/node_modules +node_modules /include/node_api_full.h /dist package-lock.json diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 2d1d9929..00000000 --- a/.npmignore +++ /dev/null @@ -1,17 +0,0 @@ -node_modules -package-lock.json -tsconfig* -.vscode -.cgenbuild -/example -/dist/library_napi_runtime* -/lib -/docs -/script -/test -.eslint* -.gitignore -cgen.config.js -README_* -.github -emsdk-cache diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index ac78eba3..4ac9e2f2 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -2,7 +2,7 @@ "env": { "includePath": [ "${default}", - "${workspaceFolder}/include" + "${workspaceFolder}/packages/emnapi/include" ], "defines": ["NAPI_DISABLE_CPP_EXCEPTIONS", "NODE_ADDON_API_ENABLE_MAYBE"], "clPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29910\\bin\\Hostx64\\x64\\cl.exe", diff --git a/.vscode/launch.json b/.vscode/launch.json index 3e2831e1..4fb8aec6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "request": "launch", "name": "Launch Program", "runtimeArgs": ["--expose-gc"], - "program": "${workspaceFolder}/test/emnapi/emnapi.test.js", + "program": "${workspaceFolder}/packages/test/emnapi/emnapi.test.js", "args": [] }, { diff --git a/.vscode/settings.json b/.vscode/settings.json index 18ed79c2..2f415ff2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "files.associations": { - "api-extractor.json": "jsonc" + "**/api-extractor.json": "jsonc" } } diff --git a/LICENSE b/LICENSE index 72f7277c..05a59441 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Toyobayashi +Copyright (c) 2021-present Toyobayashi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/example/node_modules/@tybys/emnapi/CMakeLists.txt b/example/node_modules/@tybys/emnapi/CMakeLists.txt index 70bb1732..fd44bab3 100644 --- a/example/node_modules/@tybys/emnapi/CMakeLists.txt +++ b/example/node_modules/@tybys/emnapi/CMakeLists.txt @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.9) project(emnapifake) -add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../.." "${CMAKE_CURRENT_BINARY_DIR}/real") +add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../packages/emnapi" "${CMAKE_CURRENT_BINARY_DIR}/real") diff --git a/lib/runtime/tsconfig.json b/lib/runtime/tsconfig.json deleted file mode 100644 index ace71520..00000000 --- a/lib/runtime/tsconfig.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "module": "ESNext", - "downlevelIteration": true, - "declaration": true, - "removeComments": true, - "types": [], - "declarationDir": "../../dist/runtime", - "emitDeclarationOnly": true, - "lib": [ - "ES5", - "ES2015", - "ES2020.BigInt", - "ES2021.WeakRef", - "DOM" - ] - }, - "include": [ - "../typings/napi.d.ts", - "./**/*.ts" - ] -} diff --git a/package.json b/package.json index 5673a79b..8b8e266e 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,11 @@ { - "name": "@tybys/emnapi", + "private": true, "version": "0.10.0", - "description": "Node-API implementation for Emscripten", - "main": "index.js", - "typings": "index.d.ts", "scripts": { - "clean": "cd test && cgen clean", - "build:runtime": "tsc -p ./lib/runtime/tsconfig.json && node ./script/build-runtime.js", - "build:lib": "npm run build:runtime && tsc && node ./script/build.js", + "build": "cd ./packages/runtime&&npm run build&&cd ../emnapi&&npm run build", "release": "npm run build:runtime && tsc && node ./script/build.js&&node ./script/release.js", - "prepare": "npm run build:lib", - "rebuild": "npm run build:lib && cd test && cgen rebuild -e -d", - "rebuild:r": "npm run build:lib && cd test && cgen rebuild -e", - "rebuild:n": "cd test && cgen rebuild -d", - "rebuild:nr": "cd test && cgen rebuild", - "lint": "eslint {lib/**/*.ts,test/**/*.ts}", - "test": "node ./script/test.js", - "test:r": "cross-env NODE_ENV=production node ./script/test.js", - "test:n": "cross-env EMNAPI_TEST_NATIVE=1 node ./script/test.js", - "test:nr": "cross-env EMNAPI_TEST_NATIVE=1 NODE_ENV=production node ./script/test.js" + "lint": "eslint packages/**/*.ts" }, - "keywords": [ - "emscripten", - "wasm", - "emcc", - "webassembly", - "bindings", - "c", - "c++", - "cpp", - "n-api", - "napi", - "node-addon-api" - ], "author": "toyobayashi", "repository": { "type": "git", @@ -47,28 +20,20 @@ }, "license": "MIT", "devDependencies": { - "@microsoft/api-extractor": "^7.19.4", - "@rollup/plugin-node-resolve": "^13.1.3", - "@rollup/plugin-typescript": "^8.3.0", - "@tybys/cgen": "^0.11.1", "@tybys/cross-zip": "^3.1.0", "@types/fs-extra": "^9.0.13", "@types/node": "^16.11.21", "@typescript-eslint/eslint-plugin": "^5.8.1", "@typescript-eslint/parser": "^5.8.1", - "chalk": "^4.1.1", - "cross-env": "^7.0.3", "eslint": "^8.5.0", "eslint-config-standard-with-typescript": "^21.0.1", "eslint-plugin-import": "^2.25.3", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^6.0.0", "fs-extra": "^10.0.0", - "glob": "^7.2.0", - "node-addon-api": "4.3.0", - "rollup": "^2.66.1", - "rollup-plugin-terser": "^7.0.2", - "tslib": "^2.3.1", - "typescript": "~4.5.2" - } + "typescript": "~4.5.5" + }, + "workspaces": [ + "./packages/*" + ] } diff --git a/packages/emnapi/.gitignore b/packages/emnapi/.gitignore new file mode 100644 index 00000000..bd07d4e1 --- /dev/null +++ b/packages/emnapi/.gitignore @@ -0,0 +1,2 @@ +node_modules +/dist diff --git a/CMakeLists.txt b/packages/emnapi/CMakeLists.txt similarity index 100% rename from CMakeLists.txt rename to packages/emnapi/CMakeLists.txt diff --git a/packages/emnapi/LICENSE b/packages/emnapi/LICENSE new file mode 100644 index 00000000..05a59441 --- /dev/null +++ b/packages/emnapi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021-present Toyobayashi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/packages/emnapi/README.md similarity index 100% rename from README.md rename to packages/emnapi/README.md diff --git a/README_CN.md b/packages/emnapi/README_CN.md similarity index 100% rename from README_CN.md rename to packages/emnapi/README_CN.md diff --git a/include/common.h b/packages/emnapi/include/common.h similarity index 100% rename from include/common.h rename to packages/emnapi/include/common.h diff --git a/include/emnapi.h b/packages/emnapi/include/emnapi.h similarity index 100% rename from include/emnapi.h rename to packages/emnapi/include/emnapi.h diff --git a/include/js_native_api.h b/packages/emnapi/include/js_native_api.h similarity index 100% rename from include/js_native_api.h rename to packages/emnapi/include/js_native_api.h diff --git a/include/js_native_api_types.h b/packages/emnapi/include/js_native_api_types.h similarity index 100% rename from include/js_native_api_types.h rename to packages/emnapi/include/js_native_api_types.h diff --git a/include/napi-inl.h b/packages/emnapi/include/napi-inl.h similarity index 100% rename from include/napi-inl.h rename to packages/emnapi/include/napi-inl.h diff --git a/include/napi.h b/packages/emnapi/include/napi.h similarity index 100% rename from include/napi.h rename to packages/emnapi/include/napi.h diff --git a/include/node_api.h b/packages/emnapi/include/node_api.h similarity index 100% rename from include/node_api.h rename to packages/emnapi/include/node_api.h diff --git a/index.d.ts b/packages/emnapi/index.d.ts similarity index 100% rename from index.d.ts rename to packages/emnapi/index.d.ts diff --git a/index.js b/packages/emnapi/index.js similarity index 100% rename from index.js rename to packages/emnapi/index.js diff --git a/packages/emnapi/package.json b/packages/emnapi/package.json new file mode 100644 index 00000000..7ea5e461 --- /dev/null +++ b/packages/emnapi/package.json @@ -0,0 +1,36 @@ +{ + "name": "@tybys/emnapi", + "version": "0.10.0", + "description": "Node-API implementation for Emscripten", + "main": "index.js", + "devDependencies": {}, + "scripts": { + "build": "tsc && node ./script/build.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/toyobayashi/emnapi.git" + }, + "keywords": [ + "emscripten", + "wasm", + "emcc", + "webassembly", + "bindings", + "c", + "c++", + "cpp", + "n-api", + "napi", + "node-addon-api" + ], + "author": "toyobayashi", + "license": "MIT", + "bugs": { + "url": "https://github.com/toyobayashi/emnapi/issues" + }, + "homepage": "https://github.com/toyobayashi/emnapi#readme", + "publishConfig": { + "access": "public" + } +} diff --git a/packages/emnapi/script/build.js b/packages/emnapi/script/build.js new file mode 100644 index 00000000..481ce63f --- /dev/null +++ b/packages/emnapi/script/build.js @@ -0,0 +1,72 @@ +const fs = require('fs') +const path = require('path') + +const { + runtimeOut +} = require('../../runtime/script/build.js') + +async function build () { + const libTsconfigPath = path.join(__dirname, '../tsconfig.json') + const libTsconfig = JSON.parse(fs.readFileSync(libTsconfigPath, 'utf8')) + + const libOut = path.join(path.dirname(libTsconfigPath), libTsconfig.compilerOptions.outFile) + + const runtimeCode = fs.readFileSync(runtimeOut, 'utf8') + const libCode = fs.readFileSync(libOut, 'utf8') + + fs.writeFileSync(libOut, + libCode + .replace('__EMNAPI_RUNTIME_REPLACE__', `'${runtimeCode.replace(/\\/g, '\\\\').replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}'`) + .replace('__EMNAPI_RUNTIME_INIT__;', '') + .replace(/(makeDynCall\(.*?\))/g, '{{{ $1 }}}') + /* .replace(/(makeMalloc\(.*?\))/g, '{{{ $1 }}}') */, + 'utf8' + ) + + fs.writeFileSync(path.join(path.dirname(libOut), path.basename(libOut, '.js') + '_no_runtime.js'), + libCode + .replace('__EMNAPI_RUNTIME_REPLACE__', '""') + .replace('__EMNAPI_RUNTIME_INIT__;', ` + (function () { + if ('emnapiRuntime' in Module) { + emnapi = Module.emnapiRuntime; + } + if (!emnapi && typeof require === 'function') { + try { + emnapi = require('@tybys/emnapi-runtime') + } catch (_) {} + } + if (!emnapi) { + emnapi = (function () { + let g; + g = (function () { return this })(); + + try { + g = g || new Function('return this')(); + } catch (_) { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof __webpack_public_path__ === 'undefined') { + if (typeof global !== 'undefined') return global; + } + if (typeof window !== 'undefined') return window; + if (typeof self !== 'undefined') return self; + } + + return g; + })().emnapi; + } + })();`) + .replace(/(makeDynCall\(.*?\))/g, '{{{ $1 }}}') + /* .replace(/(makeMalloc\(.*?\))/g, '{{{ $1 }}}') */, + 'utf8' + ) +} + +exports.build = build + +if (module === require.main) { + build().catch(err => { + console.error(err) + process.exit(1) + }) +} diff --git a/src/emnapi.c b/packages/emnapi/src/emnapi.c similarity index 100% rename from src/emnapi.c rename to packages/emnapi/src/emnapi.c diff --git a/lib/emnapi.ts b/packages/emnapi/src/emnapi.ts similarity index 100% rename from lib/emnapi.ts rename to packages/emnapi/src/emnapi.ts diff --git a/lib/env.ts b/packages/emnapi/src/env.ts similarity index 100% rename from lib/env.ts rename to packages/emnapi/src/env.ts diff --git a/lib/error.ts b/packages/emnapi/src/error.ts similarity index 100% rename from lib/error.ts rename to packages/emnapi/src/error.ts diff --git a/lib/function.ts b/packages/emnapi/src/function.ts similarity index 100% rename from lib/function.ts rename to packages/emnapi/src/function.ts diff --git a/lib/init.ts b/packages/emnapi/src/init.ts similarity index 100% rename from lib/init.ts rename to packages/emnapi/src/init.ts diff --git a/lib/internal.ts b/packages/emnapi/src/internal.ts similarity index 100% rename from lib/internal.ts rename to packages/emnapi/src/internal.ts diff --git a/lib/life.ts b/packages/emnapi/src/life.ts similarity index 100% rename from lib/life.ts rename to packages/emnapi/src/life.ts diff --git a/lib/memory.ts b/packages/emnapi/src/memory.ts similarity index 100% rename from lib/memory.ts rename to packages/emnapi/src/memory.ts diff --git a/lib/promise.ts b/packages/emnapi/src/promise.ts similarity index 100% rename from lib/promise.ts rename to packages/emnapi/src/promise.ts diff --git a/lib/property.ts b/packages/emnapi/src/property.ts similarity index 100% rename from lib/property.ts rename to packages/emnapi/src/property.ts diff --git a/lib/register.ts b/packages/emnapi/src/register.ts similarity index 100% rename from lib/register.ts rename to packages/emnapi/src/register.ts diff --git a/lib/script.ts b/packages/emnapi/src/script.ts similarity index 100% rename from lib/script.ts rename to packages/emnapi/src/script.ts diff --git a/lib/typings/compiler.d.ts b/packages/emnapi/src/typings/compiler.d.ts similarity index 100% rename from lib/typings/compiler.d.ts rename to packages/emnapi/src/typings/compiler.d.ts diff --git a/lib/typings/ctype.d.ts b/packages/emnapi/src/typings/ctype.d.ts similarity index 90% rename from lib/typings/ctype.d.ts rename to packages/emnapi/src/typings/ctype.d.ts index e53741f8..d7b424c6 100644 --- a/lib/typings/ctype.d.ts +++ b/packages/emnapi/src/typings/ctype.d.ts @@ -1,4 +1,6 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars declare type Pointer = number +// eslint-disable-next-line @typescript-eslint/no-unused-vars declare type PointerPointer = number declare type FunctionPointer any> = Pointer declare type Const = T diff --git a/lib/typings/runtime.d.ts b/packages/emnapi/src/typings/runtime.d.ts similarity index 97% rename from lib/typings/runtime.d.ts rename to packages/emnapi/src/typings/runtime.d.ts index 972c2d0d..2eaec702 100644 --- a/lib/typings/runtime.d.ts +++ b/packages/emnapi/src/typings/runtime.d.ts @@ -39,8 +39,8 @@ declare interface ICallbackInfo { _this: any _data: void_p _length: number - _args: any[], - _newTarget: Function | undefined, + _args: any[] + _newTarget: Function | undefined _isConstructCall: boolean } diff --git a/lib/util.ts b/packages/emnapi/src/util.ts similarity index 100% rename from lib/util.ts rename to packages/emnapi/src/util.ts diff --git a/lib/value-operation.ts b/packages/emnapi/src/value-operation.ts similarity index 100% rename from lib/value-operation.ts rename to packages/emnapi/src/value-operation.ts diff --git a/lib/value/convert2c.ts b/packages/emnapi/src/value/convert2c.ts similarity index 100% rename from lib/value/convert2c.ts rename to packages/emnapi/src/value/convert2c.ts diff --git a/lib/value/convert2napi.ts b/packages/emnapi/src/value/convert2napi.ts similarity index 100% rename from lib/value/convert2napi.ts rename to packages/emnapi/src/value/convert2napi.ts diff --git a/lib/value/create.ts b/packages/emnapi/src/value/create.ts similarity index 100% rename from lib/value/create.ts rename to packages/emnapi/src/value/create.ts diff --git a/lib/value/global.ts b/packages/emnapi/src/value/global.ts similarity index 100% rename from lib/value/global.ts rename to packages/emnapi/src/value/global.ts diff --git a/lib/version.ts b/packages/emnapi/src/version.ts similarity index 100% rename from lib/version.ts rename to packages/emnapi/src/version.ts diff --git a/lib/wrap.ts b/packages/emnapi/src/wrap.ts similarity index 100% rename from lib/wrap.ts rename to packages/emnapi/src/wrap.ts diff --git a/tsconfig.base.json b/packages/emnapi/tsconfig.base.json similarity index 100% rename from tsconfig.base.json rename to packages/emnapi/tsconfig.base.json diff --git a/tsconfig.prod.json b/packages/emnapi/tsconfig.json similarity index 53% rename from tsconfig.prod.json rename to packages/emnapi/tsconfig.json index 382e881f..98512011 100644 --- a/tsconfig.prod.json +++ b/packages/emnapi/tsconfig.json @@ -1,10 +1,9 @@ { "extends": "./tsconfig.base.json", "compilerOptions": { - "module": "ESNext", - "downlevelIteration": true, - "removeComments": true, - "declaration": false, + "outFile": "./dist/library_napi.js", + "importHelpers": false, + "noEmitHelpers": true, "types": [], "lib": [ "ES5", @@ -15,7 +14,8 @@ ] }, "include": [ - "./lib/typings/napi.d.ts", - "./lib/runtime/**/*.ts" + "../runtime/dist/emnapi.d.ts", + "../runtime/src/napi.d.ts", + "./src/**/*.ts" ] } diff --git a/packages/runtime/.gitignore b/packages/runtime/.gitignore new file mode 100644 index 00000000..206bfb24 --- /dev/null +++ b/packages/runtime/.gitignore @@ -0,0 +1,3 @@ +/lib +node_modules +/dist diff --git a/packages/runtime/.npmignore b/packages/runtime/.npmignore new file mode 100644 index 00000000..14001ca6 --- /dev/null +++ b/packages/runtime/.npmignore @@ -0,0 +1,9 @@ +.vscode +node_modules +/script +/lib/**/*.d.ts +/src +.gitignore +.npmignore +api-extractor.json +tsconfig.json diff --git a/packages/runtime/LICENSE b/packages/runtime/LICENSE new file mode 100644 index 00000000..05a59441 --- /dev/null +++ b/packages/runtime/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021-present Toyobayashi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/runtime/api-extractor.json b/packages/runtime/api-extractor.json similarity index 90% rename from lib/runtime/api-extractor.json rename to packages/runtime/api-extractor.json index bd585884..c2f65d7b 100644 --- a/lib/runtime/api-extractor.json +++ b/packages/runtime/api-extractor.json @@ -4,14 +4,14 @@ // "extends": "./shared/api-extractor-base.json" // "extends": "my-package/include/api-extractor-base.json" - "projectFolder": "../..", + "projectFolder": ".", - "mainEntryPointFilePath": "/dist/runtime/index.d.ts", + "mainEntryPointFilePath": "/lib/index.d.ts", "bundledPackages": [], "compiler": { - "tsconfigFilePath": "/lib/runtime/tsconfig.json" + "tsconfigFilePath": "/tsconfig.json" // "overrideTsconfig": { // . . . @@ -43,7 +43,7 @@ // "betaTrimmedFilePath": "/dist/-beta.d.ts", - "publicTrimmedFilePath": "/dist/library_napi_runtime.d.ts" + "publicTrimmedFilePath": "/dist/emnapi.d.ts" // "omitTrimmingComments": true }, diff --git a/packages/runtime/index.js b/packages/runtime/index.js new file mode 100644 index 00000000..1b71a27d --- /dev/null +++ b/packages/runtime/index.js @@ -0,0 +1,5 @@ +if (process.env.NODE_ENV === 'production') { + module.exports = require('./dist/emnapi.min.js') +} else { + module.exports = require('./dist/emnapi.js') +} diff --git a/packages/runtime/package.json b/packages/runtime/package.json new file mode 100644 index 00000000..5e5a39e0 --- /dev/null +++ b/packages/runtime/package.json @@ -0,0 +1,32 @@ +{ + "name": "@tybys/emnapi-runtime", + "version": "0.10.0", + "description": "emnapi runtime", + "main": "index.js", + "module": "./lib/index.js", + "typings": "./dist/emnapi.d.ts", + "devDependencies": { + "@microsoft/api-extractor": "^7.19.4", + "@rollup/plugin-node-resolve": "^13.1.3", + "rollup": "^2.66.1", + "rollup-plugin-terser": "^7.0.2", + "tslib": "^2.3.1" + }, + "scripts": { + "build": "tsc -p ./tsconfig.json && node ./script/build.js", + "prepare": "npm run build" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/toyobayashi/emnapi.git" + }, + "author": "toyobayashi", + "license": "MIT", + "bugs": { + "url": "https://github.com/toyobayashi/emnapi/issues" + }, + "homepage": "https://github.com/toyobayashi/emnapi#readme", + "publishConfig": { + "access": "public" + } +} diff --git a/packages/runtime/script/build.js b/packages/runtime/script/build.js new file mode 100644 index 00000000..65a4d143 --- /dev/null +++ b/packages/runtime/script/build.js @@ -0,0 +1,97 @@ +const path = require('path') +const fs = require('fs-extra') +const rollup = require('rollup') +const rollupNodeResolve = require('@rollup/plugin-node-resolve').default +const rollupTerser = require('rollup-plugin-terser').terser +const runtimeOut = path.join(__dirname, '../dist/library_napi_runtime.js') + +function build () { + const { + Extractor, + ExtractorConfig + } = require('@microsoft/api-extractor') + const apiExtractorJsonPath = path.join(__dirname, '../api-extractor.json') + const extractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath) + const extractorResult = Extractor.invoke(extractorConfig, { + localBuild: true, + showVerboseMessages: true + }) + if (extractorResult.succeeded) { + console.log('API Extractor completed successfully') + } else { + const errmsg = `API Extractor completed with ${extractorResult.errorCount} errors and ${extractorResult.warningCount} warnings` + return Promise.reject(new Error(errmsg)) + } + + const runtimeDts = extractorConfig.publicTrimmedFilePath + + fs.appendFileSync(runtimeDts, '\nexport as namespace emnapi;\n', 'utf8') + + /** + * @param {boolean=} minify + * @returns {rollup.RollupOptions} + */ + function createInput (minify) { + return { + input: path.join(__dirname, '../lib/index.js'), + plugins: [ + rollupNodeResolve({ + mainFields: ['module', 'main'] + }), + ...(minify + ? [ + rollupTerser({ + compress: true, + mangle: true, + format: { + comments: false + } + }) + ] + : []) + ] + } + } + + return Promise.all(([ + { + input: createInput(false), + output: { + file: runtimeOut, + format: 'iife', + name: 'emnapi', + exports: 'named' + } + }, + { + input: createInput(false), + output: { + file: path.join(path.dirname(runtimeOut), 'emnapi.js'), + format: 'umd', + name: 'emnapi', + exports: 'named' + } + }, + { + input: createInput(true), + output: { + file: path.join(path.dirname(runtimeOut), 'emnapi.min.js'), + format: 'umd', + name: 'emnapi', + exports: 'named' + } + } + ]).map(conf => { + return rollup.rollup(conf.input).then(bundle => bundle.write(conf.output)) + })) +} + +exports.build = build +exports.runtimeOut = runtimeOut + +if (module === require.main) { + build().catch(err => { + console.error(err) + process.exit(1) + }) +} diff --git a/lib/runtime/Deferred.ts b/packages/runtime/src/Deferred.ts similarity index 100% rename from lib/runtime/Deferred.ts rename to packages/runtime/src/Deferred.ts diff --git a/lib/runtime/Handle.ts b/packages/runtime/src/Handle.ts similarity index 100% rename from lib/runtime/Handle.ts rename to packages/runtime/src/Handle.ts diff --git a/lib/runtime/HandleScope.ts b/packages/runtime/src/HandleScope.ts similarity index 100% rename from lib/runtime/HandleScope.ts rename to packages/runtime/src/HandleScope.ts diff --git a/lib/runtime/LinkedList.ts b/packages/runtime/src/LinkedList.ts similarity index 100% rename from lib/runtime/LinkedList.ts rename to packages/runtime/src/LinkedList.ts diff --git a/lib/runtime/Reference.ts b/packages/runtime/src/Reference.ts similarity index 100% rename from lib/runtime/Reference.ts rename to packages/runtime/src/Reference.ts diff --git a/lib/runtime/Store.ts b/packages/runtime/src/Store.ts similarity index 100% rename from lib/runtime/Store.ts rename to packages/runtime/src/Store.ts diff --git a/lib/runtime/env.ts b/packages/runtime/src/env.ts similarity index 100% rename from lib/runtime/env.ts rename to packages/runtime/src/env.ts diff --git a/lib/runtime/index.ts b/packages/runtime/src/index.ts similarity index 100% rename from lib/runtime/index.ts rename to packages/runtime/src/index.ts diff --git a/lib/typings/napi.d.ts b/packages/runtime/src/napi.d.ts similarity index 94% rename from lib/typings/napi.d.ts rename to packages/runtime/src/napi.d.ts index dc491980..2dcc4aad 100644 --- a/lib/typings/napi.d.ts +++ b/packages/runtime/src/napi.d.ts @@ -1,5 +1,3 @@ -/// - declare type napi_env = Pointer declare type napi_value = Pointer @@ -16,8 +14,8 @@ declare type napi_callback = FunctionPointer<(env: napi_env, info: napi_callback declare interface napi_extended_error_info { error_message: const_char_p engine_reserved: void_p - engine_error_code: uint32_t; - error_code: number; + engine_error_code: uint32_t + error_code: number } declare interface napi_property_descriptor { diff --git a/lib/runtime/type.ts b/packages/runtime/src/type.ts similarity index 100% rename from lib/runtime/type.ts rename to packages/runtime/src/type.ts diff --git a/lib/runtime/util.ts b/packages/runtime/src/util.ts similarity index 100% rename from lib/runtime/util.ts rename to packages/runtime/src/util.ts diff --git a/packages/runtime/tsconfig.json b/packages/runtime/tsconfig.json new file mode 100644 index 00000000..bb7867ae --- /dev/null +++ b/packages/runtime/tsconfig.json @@ -0,0 +1,36 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "ESNext", + "noImplicitAny": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noImplicitUseStrict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "strictNullChecks": true, + "strictPropertyInitialization": true, + "strictBindCallApply": true, + "strictFunctionTypes": true, + "downlevelIteration": true, + "noEmitHelpers": true, + "importHelpers": true, + "declaration": true, + "outDir": "./lib", + "removeComments": true, + "paths": { + "tslib" : ["./node_modules/tslib/tslib.d.ts"] + }, + "lib": [ + "ES5", + "ES2015", + "ES2020.BigInt", + "ES2021.WeakRef", + "DOM" + ] + }, + "include": [ + "../emnapi/src/typings/ctype.d.ts", + "./src/**/*.ts" + ] +} diff --git a/packages/test/.gitignore b/packages/test/.gitignore new file mode 100644 index 00000000..22f110fd --- /dev/null +++ b/packages/test/.gitignore @@ -0,0 +1,4 @@ +.cgenbuild +node_modules +/emnapi +/CMakeLists.txt diff --git a/test/arg/arg.test.js b/packages/test/arg/arg.test.js similarity index 100% rename from test/arg/arg.test.js rename to packages/test/arg/arg.test.js diff --git a/test/arg/binding.c b/packages/test/arg/binding.c similarity index 100% rename from test/arg/binding.c rename to packages/test/arg/binding.c diff --git a/test/array/array.test.js b/packages/test/array/array.test.js similarity index 100% rename from test/array/array.test.js rename to packages/test/array/array.test.js diff --git a/test/array/binding.c b/packages/test/array/binding.c similarity index 100% rename from test/array/binding.c rename to packages/test/array/binding.c diff --git a/test/bigint/bigint.test.js b/packages/test/bigint/bigint.test.js similarity index 100% rename from test/bigint/bigint.test.js rename to packages/test/bigint/bigint.test.js diff --git a/test/bigint/binding.c b/packages/test/bigint/binding.c similarity index 100% rename from test/bigint/binding.c rename to packages/test/bigint/binding.c diff --git a/test/callback/binding.c b/packages/test/callback/binding.c similarity index 100% rename from test/callback/binding.c rename to packages/test/callback/binding.c diff --git a/test/callback/callback.test.js b/packages/test/callback/callback.test.js similarity index 100% rename from test/callback/callback.test.js rename to packages/test/callback/callback.test.js diff --git a/packages/test/cgen.config.js b/packages/test/cgen.config.js new file mode 100644 index 00000000..488a39da --- /dev/null +++ b/packages/test/cgen.config.js @@ -0,0 +1,108 @@ +module.exports = function (_options, { isDebug, isEmscripten }) { + const compilerFlags = isEmscripten + ? [ + // ...(isDebug ? ['-sDISABLE_EXCEPTION_CATCHING=0'] : []) + ] + : [] + + const linkerFlags = isEmscripten + ? [ + // "-sEXPORTED_FUNCTIONS=['_malloc','_free']", + '-sALLOW_MEMORY_GROWTH=1', + ...(isDebug ? ['-sSAFE_HEAP=1'/* , '-sDISABLE_EXCEPTION_CATCHING=0' */] : []) + ] + : [] + + const includePaths = isEmscripten + ? ['../emnapi/include'] + : [`${require('path').join(require('os').homedir(), 'AppData/Local/node-gyp/Cache', process.versions.node, 'include/node')}`, '../node_modules/node-addon-api'] + + const createTarget = (name, sources, needEntry) => ({ + name: name, + type: isEmscripten ? 'exe' : 'node', + sources: [...(needEntry ? (sources.push('./entry_point.c'), sources) : sources)], + emwrap: { + exports: ['emnapi'] + }, + includePaths, + libs: ['testcommon', ...(isEmscripten ? ['emnapi'] : [])], + compileOptions: [...compilerFlags], + // eslint-disable-next-line no-template-curly-in-string + linkOptions: [...linkerFlags] + }) + + const createNodeAddonApiTarget = (name, sources) => ({ + name: name, + type: isEmscripten ? 'exe' : 'node', + sources: [...sources], + emwrap: { + exports: ['emnapi'] + }, + includePaths, + libs: [...(isEmscripten ? ['emnapi'] : [])], + defines: ['NAPI_DISABLE_CPP_EXCEPTIONS', 'NODE_ADDON_API_ENABLE_MAYBE'], + compileOptions: [...compilerFlags], + // eslint-disable-next-line no-template-curly-in-string + linkOptions: [...linkerFlags] + }) + + return { + project: 'emnapitest', + // dependencies: isEmscripten ? { '../emnapi': { EMNAPI_LIB_NO_RUNTIME: 'ON' } } : {}, + targets: [ + ...(isEmscripten + ? [{ + type: 'lib', + name: 'emnapi', + sources: ['../emnapi/src/emnapi.c'], + includePaths, + compileOptions: [...compilerFlags], + linkOptions: [...linkerFlags], + publicLinkOptions: [`--js-library=${require('path').join(__dirname, '../emnapi/dist/library_napi_no_runtime.js')}`] + }] + : []), + { + type: 'lib', + name: 'testcommon', + sources: ['./common.c'], + includePaths, + compileOptions: [...compilerFlags], + linkOptions: [...linkerFlags] + }, + createTarget('env', ['./env/binding.c']), + createTarget('hello', ['./hello/binding.c']), + createTarget('arg', ['./arg/binding.c'], true), + createTarget('callback', ['./callback/binding.c'], true), + createTarget('objfac', ['./objfac/binding.c'], true), + createTarget('fnfac', ['./fnfac/binding.c'], true), + createTarget('general', ['./general/binding.c'], true), + createTarget('string', ['./string/binding.c'], true), + createTarget('property', ['./property/binding.c'], true), + createTarget('promise', ['./promise/binding.c'], true), + createTarget('object', ['./object/test_null.c', './object/test_object.c'], true), + createTarget('objwrap', ['./objwrap/myobject.cc'], true), + createTarget('bigint', ['./bigint/binding.c'], true), + createTarget('fnwrap', ['./fnwrap/myobject.cc', './fnwrap/binding.cc'], true), + createTarget('passwrap', ['./passwrap/myobject.cc', './passwrap/binding.cc'], true), + createTarget('array', ['./array/binding.c'], true), + createTarget('constructor', ['./constructor/binding.c'], true), + createTarget('conversion', ['./conversion/test_conversions.c', './conversion/test_null.c'], true), + createTarget('dataview', ['./dataview/binding.c'], true), + createTarget('date', ['./date/binding.c'], true), + createTarget('error', ['./error/binding.c'], true), + createTarget('exception', ['./exception/binding.c'], true), + createTarget('ref', ['./ref/binding.c'], true), + createTarget('ref_double_free', ['./ref_double_free/binding.c'], true), + createTarget('function', ['./function/binding.c'], true), + createTarget('scope', ['./scope/binding.c'], true), + createTarget('newtarget', ['./newtarget/binding.c'], true), + createTarget('number', ['./number/binding.c'], true), + createTarget('symbol', ['./symbol/binding.c'], true), + createTarget('typedarray', ['./typedarray/binding.c'], true), + ...(isEmscripten ? [createTarget('emnapitest', ['./emnapitest/binding.c'], true)] : []), + createTarget('version', ['./version/binding.c']), + + createNodeAddonApiTarget('n_hello', ['./node-addon-api/hello/binding.cc']) + ] + } +} diff --git a/test/common.c b/packages/test/common.c similarity index 100% rename from test/common.c rename to packages/test/common.c diff --git a/test/common.h b/packages/test/common.h similarity index 100% rename from test/common.h rename to packages/test/common.h diff --git a/test/common.js b/packages/test/common.js similarity index 100% rename from test/common.js rename to packages/test/common.js diff --git a/test/constructor/binding.c b/packages/test/constructor/binding.c similarity index 100% rename from test/constructor/binding.c rename to packages/test/constructor/binding.c diff --git a/test/constructor/constructor.test.js b/packages/test/constructor/constructor.test.js similarity index 100% rename from test/constructor/constructor.test.js rename to packages/test/constructor/constructor.test.js diff --git a/test/conversion/conversion.test.js b/packages/test/conversion/conversion.test.js similarity index 100% rename from test/conversion/conversion.test.js rename to packages/test/conversion/conversion.test.js diff --git a/test/conversion/test_conversions.c b/packages/test/conversion/test_conversions.c similarity index 100% rename from test/conversion/test_conversions.c rename to packages/test/conversion/test_conversions.c diff --git a/test/conversion/test_null.c b/packages/test/conversion/test_null.c similarity index 100% rename from test/conversion/test_null.c rename to packages/test/conversion/test_null.c diff --git a/test/conversion/test_null.h b/packages/test/conversion/test_null.h similarity index 100% rename from test/conversion/test_null.h rename to packages/test/conversion/test_null.h diff --git a/test/dataview/binding.c b/packages/test/dataview/binding.c similarity index 100% rename from test/dataview/binding.c rename to packages/test/dataview/binding.c diff --git a/test/dataview/dataview.test.js b/packages/test/dataview/dataview.test.js similarity index 100% rename from test/dataview/dataview.test.js rename to packages/test/dataview/dataview.test.js diff --git a/test/date/binding.c b/packages/test/date/binding.c similarity index 100% rename from test/date/binding.c rename to packages/test/date/binding.c diff --git a/test/date/date.test.js b/packages/test/date/date.test.js similarity index 100% rename from test/date/date.test.js rename to packages/test/date/date.test.js diff --git a/test/emnapitest/binding.c b/packages/test/emnapitest/binding.c similarity index 100% rename from test/emnapitest/binding.c rename to packages/test/emnapitest/binding.c diff --git a/test/emnapitest/emnapi.test.js b/packages/test/emnapitest/emnapi.test.js similarity index 100% rename from test/emnapitest/emnapi.test.js rename to packages/test/emnapitest/emnapi.test.js diff --git a/test/entry_point.c b/packages/test/entry_point.c similarity index 100% rename from test/entry_point.c rename to packages/test/entry_point.c diff --git a/test/env/binding.c b/packages/test/env/binding.c similarity index 100% rename from test/env/binding.c rename to packages/test/env/binding.c diff --git a/test/env/env.test.js b/packages/test/env/env.test.js similarity index 100% rename from test/env/env.test.js rename to packages/test/env/env.test.js diff --git a/test/error/binding.c b/packages/test/error/binding.c similarity index 100% rename from test/error/binding.c rename to packages/test/error/binding.c diff --git a/test/error/error.test.js b/packages/test/error/error.test.js similarity index 100% rename from test/error/error.test.js rename to packages/test/error/error.test.js diff --git a/test/exception/binding.c b/packages/test/exception/binding.c similarity index 100% rename from test/exception/binding.c rename to packages/test/exception/binding.c diff --git a/test/exception/exception.test.js b/packages/test/exception/exception.test.js similarity index 100% rename from test/exception/exception.test.js rename to packages/test/exception/exception.test.js diff --git a/test/fnfac/binding.c b/packages/test/fnfac/binding.c similarity index 100% rename from test/fnfac/binding.c rename to packages/test/fnfac/binding.c diff --git a/test/fnfac/fnfac.test.js b/packages/test/fnfac/fnfac.test.js similarity index 100% rename from test/fnfac/fnfac.test.js rename to packages/test/fnfac/fnfac.test.js diff --git a/test/fnwrap/binding.cc b/packages/test/fnwrap/binding.cc similarity index 100% rename from test/fnwrap/binding.cc rename to packages/test/fnwrap/binding.cc diff --git a/test/fnwrap/fnwrap.test.js b/packages/test/fnwrap/fnwrap.test.js similarity index 100% rename from test/fnwrap/fnwrap.test.js rename to packages/test/fnwrap/fnwrap.test.js diff --git a/test/fnwrap/myobject.cc b/packages/test/fnwrap/myobject.cc similarity index 100% rename from test/fnwrap/myobject.cc rename to packages/test/fnwrap/myobject.cc diff --git a/test/fnwrap/myobject.h b/packages/test/fnwrap/myobject.h similarity index 100% rename from test/fnwrap/myobject.h rename to packages/test/fnwrap/myobject.h diff --git a/test/function/binding.c b/packages/test/function/binding.c similarity index 100% rename from test/function/binding.c rename to packages/test/function/binding.c diff --git a/test/function/function.test.js b/packages/test/function/function.test.js similarity index 100% rename from test/function/function.test.js rename to packages/test/function/function.test.js diff --git a/test/general/binding.c b/packages/test/general/binding.c similarity index 100% rename from test/general/binding.c rename to packages/test/general/binding.c diff --git a/test/general/finalizer.test.js b/packages/test/general/finalizer.test.js similarity index 100% rename from test/general/finalizer.test.js rename to packages/test/general/finalizer.test.js diff --git a/test/general/general.test.js b/packages/test/general/general.test.js similarity index 100% rename from test/general/general.test.js rename to packages/test/general/general.test.js diff --git a/test/general/global.test.js b/packages/test/general/global.test.js similarity index 100% rename from test/general/global.test.js rename to packages/test/general/global.test.js diff --git a/test/general/run.test.js b/packages/test/general/run.test.js similarity index 100% rename from test/general/run.test.js rename to packages/test/general/run.test.js diff --git a/test/general/status.test.js b/packages/test/general/status.test.js similarity index 100% rename from test/general/status.test.js rename to packages/test/general/status.test.js diff --git a/test/hello/binding.c b/packages/test/hello/binding.c similarity index 100% rename from test/hello/binding.c rename to packages/test/hello/binding.c diff --git a/test/hello/hello.test.js b/packages/test/hello/hello.test.js similarity index 89% rename from test/hello/hello.test.js rename to packages/test/hello/hello.test.js index 82d4d07c..8a4887bc 100644 --- a/test/hello/hello.test.js +++ b/packages/test/hello/hello.test.js @@ -21,7 +21,7 @@ module.exports = new Promise((resolve, reject) => { function load (request) { const mod = require(request) - return typeof mod.default === 'function' ? mod.default({ emnapiRuntime: require(${JSON.stringify(join(__dirname, '../../dist/emnapi.min.js'))}) }).then(({ Module }) => Module.emnapiExports) : Promise.resolve(mod) + return typeof mod.default === 'function' ? mod.default({ emnapiRuntime: require(${JSON.stringify(require.resolve('@tybys/emnapi-runtime'))}) }).then(({ Module }) => Module.emnapiExports) : Promise.resolve(mod) } load(${JSON.stringify(getEntry('hello'))}).then((binding) => { const msg = binding.hello(); parentPort.postMessage(msg) });`, { eval: true, env: process.env }) .on('message', common.mustCall((msg) => { diff --git a/test/newtarget/binding.c b/packages/test/newtarget/binding.c similarity index 100% rename from test/newtarget/binding.c rename to packages/test/newtarget/binding.c diff --git a/test/newtarget/newtarget.test.js b/packages/test/newtarget/newtarget.test.js similarity index 100% rename from test/newtarget/newtarget.test.js rename to packages/test/newtarget/newtarget.test.js diff --git a/test/node-addon-api/hello/binding.cc b/packages/test/node-addon-api/hello/binding.cc similarity index 100% rename from test/node-addon-api/hello/binding.cc rename to packages/test/node-addon-api/hello/binding.cc diff --git a/test/node-addon-api/hello/hello.test.js b/packages/test/node-addon-api/hello/hello.test.js similarity index 100% rename from test/node-addon-api/hello/hello.test.js rename to packages/test/node-addon-api/hello/hello.test.js diff --git a/test/number/binding.c b/packages/test/number/binding.c similarity index 100% rename from test/number/binding.c rename to packages/test/number/binding.c diff --git a/test/number/number.test.js b/packages/test/number/number.test.js similarity index 100% rename from test/number/number.test.js rename to packages/test/number/number.test.js diff --git a/test/object/object.test.js b/packages/test/object/object.test.js similarity index 100% rename from test/object/object.test.js rename to packages/test/object/object.test.js diff --git a/test/object/object_null.test.js b/packages/test/object/object_null.test.js similarity index 100% rename from test/object/object_null.test.js rename to packages/test/object/object_null.test.js diff --git a/test/object/test_null.c b/packages/test/object/test_null.c similarity index 100% rename from test/object/test_null.c rename to packages/test/object/test_null.c diff --git a/test/object/test_null.h b/packages/test/object/test_null.h similarity index 100% rename from test/object/test_null.h rename to packages/test/object/test_null.h diff --git a/test/object/test_object.c b/packages/test/object/test_object.c similarity index 100% rename from test/object/test_object.c rename to packages/test/object/test_object.c diff --git a/test/objfac/binding.c b/packages/test/objfac/binding.c similarity index 100% rename from test/objfac/binding.c rename to packages/test/objfac/binding.c diff --git a/test/objfac/objfac.test.js b/packages/test/objfac/objfac.test.js similarity index 100% rename from test/objfac/objfac.test.js rename to packages/test/objfac/objfac.test.js diff --git a/test/objwrap/myobject.cc b/packages/test/objwrap/myobject.cc similarity index 100% rename from test/objwrap/myobject.cc rename to packages/test/objwrap/myobject.cc diff --git a/test/objwrap/myobject.h b/packages/test/objwrap/myobject.h similarity index 100% rename from test/objwrap/myobject.h rename to packages/test/objwrap/myobject.h diff --git a/test/objwrap/objwrap.test.js b/packages/test/objwrap/objwrap.test.js similarity index 100% rename from test/objwrap/objwrap.test.js rename to packages/test/objwrap/objwrap.test.js diff --git a/packages/test/package.json b/packages/test/package.json new file mode 100644 index 00000000..2aa7e986 --- /dev/null +++ b/packages/test/package.json @@ -0,0 +1,47 @@ +{ + "name": "@tybys/emnapi-test", + "private": true, + "version": "0.10.0", + "description": "emnapi test", + "main": "index.js", + "devDependencies": { + "@tybys/cgen": "^0.11.1", + "chalk": "^4.1.1", + "cross-env": "^7.0.3", + "glob": "^7.2.0", + "node-addon-api": "4.3.0" + }, + "scripts": { + "rebuild": "cd ../..&&npm run build&&cd ./packages/test && cgen rebuild -e -d", + "rebuild:r": "cd ../..&&npm run build&&cd ./packages/test && cgen rebuild -e", + "rebuild:n": "cgen rebuild -d", + "rebuild:nr": "cgen rebuild", + "test": "node ./script/test.js", + "test:r": "cross-env NODE_ENV=production node ./script/test.js", + "test:n": "cross-env EMNAPI_TEST_NATIVE=1 node ./script/test.js", + "test:nr": "cross-env EMNAPI_TEST_NATIVE=1 NODE_ENV=production node ./script/test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/toyobayashi/emnapi.git" + }, + "keywords": [ + "emscripten", + "wasm", + "emcc", + "webassembly", + "bindings", + "c", + "c++", + "cpp", + "n-api", + "napi", + "node-addon-api" + ], + "author": "toyobayashi", + "license": "MIT", + "bugs": { + "url": "https://github.com/toyobayashi/emnapi/issues" + }, + "homepage": "https://github.com/toyobayashi/emnapi#readme" +} diff --git a/test/passwrap/binding.cc b/packages/test/passwrap/binding.cc similarity index 100% rename from test/passwrap/binding.cc rename to packages/test/passwrap/binding.cc diff --git a/test/passwrap/myobject.cc b/packages/test/passwrap/myobject.cc similarity index 100% rename from test/passwrap/myobject.cc rename to packages/test/passwrap/myobject.cc diff --git a/test/passwrap/myobject.h b/packages/test/passwrap/myobject.h similarity index 100% rename from test/passwrap/myobject.h rename to packages/test/passwrap/myobject.h diff --git a/test/passwrap/passwrap.test.js b/packages/test/passwrap/passwrap.test.js similarity index 100% rename from test/passwrap/passwrap.test.js rename to packages/test/passwrap/passwrap.test.js diff --git a/test/promise/binding.c b/packages/test/promise/binding.c similarity index 100% rename from test/promise/binding.c rename to packages/test/promise/binding.c diff --git a/test/promise/promise.test.js b/packages/test/promise/promise.test.js similarity index 100% rename from test/promise/promise.test.js rename to packages/test/promise/promise.test.js diff --git a/test/property/binding.c b/packages/test/property/binding.c similarity index 100% rename from test/property/binding.c rename to packages/test/property/binding.c diff --git a/test/property/property.test.js b/packages/test/property/property.test.js similarity index 100% rename from test/property/property.test.js rename to packages/test/property/property.test.js diff --git a/test/ref/binding.c b/packages/test/ref/binding.c similarity index 100% rename from test/ref/binding.c rename to packages/test/ref/binding.c diff --git a/test/ref/ref.test.js b/packages/test/ref/ref.test.js similarity index 100% rename from test/ref/ref.test.js rename to packages/test/ref/ref.test.js diff --git a/test/ref_double_free/binding.c b/packages/test/ref_double_free/binding.c similarity index 100% rename from test/ref_double_free/binding.c rename to packages/test/ref_double_free/binding.c diff --git a/test/ref_double_free/ref_double_free.test.js b/packages/test/ref_double_free/ref_double_free.test.js similarity index 100% rename from test/ref_double_free/ref_double_free.test.js rename to packages/test/ref_double_free/ref_double_free.test.js diff --git a/test/scope/binding.c b/packages/test/scope/binding.c similarity index 100% rename from test/scope/binding.c rename to packages/test/scope/binding.c diff --git a/test/scope/scope.test.js b/packages/test/scope/scope.test.js similarity index 100% rename from test/scope/scope.test.js rename to packages/test/scope/scope.test.js diff --git a/script/test-entry.js b/packages/test/script/test-entry.js similarity index 100% rename from script/test-entry.js rename to packages/test/script/test-entry.js diff --git a/script/test.js b/packages/test/script/test.js similarity index 75% rename from script/test.js rename to packages/test/script/test.js index 12ae0c99..de09b1b7 100644 --- a/script/test.js +++ b/packages/test/script/test.js @@ -3,7 +3,7 @@ const glob = require('glob') const cwd = require('path').join(__dirname, '..') -const files = glob.sync('test/**/*.test.js', { cwd, ignore: process.env.EMNAPI_TEST_NATIVE ? 'test/emnapitest/**/*' : [] }) +const files = glob.sync('**/*.test.js', { cwd, ignore: process.env.EMNAPI_TEST_NATIVE ? 'test/emnapitest/**/*' : [] }) files.forEach((f) => { test(f) diff --git a/test/string/binding.c b/packages/test/string/binding.c similarity index 100% rename from test/string/binding.c rename to packages/test/string/binding.c diff --git a/test/string/string.test.js b/packages/test/string/string.test.js similarity index 100% rename from test/string/string.test.js rename to packages/test/string/string.test.js diff --git a/test/symbol/binding.c b/packages/test/symbol/binding.c similarity index 100% rename from test/symbol/binding.c rename to packages/test/symbol/binding.c diff --git a/test/symbol/symbol.test.js b/packages/test/symbol/symbol.test.js similarity index 100% rename from test/symbol/symbol.test.js rename to packages/test/symbol/symbol.test.js diff --git a/test/tmpdir.js b/packages/test/tmpdir.js similarity index 100% rename from test/tmpdir.js rename to packages/test/tmpdir.js diff --git a/test/typedarray/binding.c b/packages/test/typedarray/binding.c similarity index 100% rename from test/typedarray/binding.c rename to packages/test/typedarray/binding.c diff --git a/test/typedarray/typedarray.test.js b/packages/test/typedarray/typedarray.test.js similarity index 100% rename from test/typedarray/typedarray.test.js rename to packages/test/typedarray/typedarray.test.js diff --git a/test/util.js b/packages/test/util.js similarity index 93% rename from test/util.js rename to packages/test/util.js index 1b4c2252..fce9c6d8 100644 --- a/test/util.js +++ b/packages/test/util.js @@ -15,7 +15,7 @@ exports.load = function (targetName) { if (typeof mod.default === 'function') { const p = new Promise((resolve, reject) => { mod.default({ - emnapiRuntime: require('../dist/emnapi.min.js'), + emnapiRuntime: require('@tybys/emnapi-runtime'), onEmnapiInitialized: (err) => { if (err) { reject(err) diff --git a/test/version/binding.c b/packages/test/version/binding.c similarity index 100% rename from test/version/binding.c rename to packages/test/version/binding.c diff --git a/test/version/version.test.js b/packages/test/version/version.test.js similarity index 100% rename from test/version/version.test.js rename to packages/test/version/version.test.js diff --git a/script/build-runtime.js b/script/build-runtime.js deleted file mode 100644 index 3e82e28d..00000000 --- a/script/build-runtime.js +++ /dev/null @@ -1,92 +0,0 @@ -const path = require('path') -const fs = require('fs-extra') -const rollup = require('rollup') -const rollupTypescript = require('@rollup/plugin-typescript') -const rollupNodeResolve = require('@rollup/plugin-node-resolve').default -const rollupTerser = require('rollup-plugin-terser').terser -const { runtimeOut } = require('./common.js') - -const { - Extractor, - ExtractorConfig -} = require('@microsoft/api-extractor') -const apiExtractorJsonPath = path.join(__dirname, '../lib/runtime/api-extractor.json') -const extractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath) -const extractorResult = Extractor.invoke(extractorConfig, { - localBuild: true, - showVerboseMessages: true -}) -if (extractorResult.succeeded) { - console.log('API Extractor completed successfully') -} else { - console.error(`API Extractor completed with ${extractorResult.errorCount} errors and ${extractorResult.warningCount} warnings`) - process.exit(1) -} - -const runtimeDts = extractorConfig.publicTrimmedFilePath - -fs.removeSync(path.join(__dirname, '../dist/runtime')) -fs.appendFileSync(runtimeDts, '\nexport as namespace emnapi;\n', 'utf8') - -const runtimeTsconfigPath = path.join(__dirname, '../tsconfig.prod.json') - -/** - * @param {boolean=} minify - * @returns {rollup.RollupOptions} - */ -function createInput (minify) { - return { - input: path.join(__dirname, '../lib/runtime/index.ts'), - plugins: [ - rollupNodeResolve({ - mainFields: ['module', 'main'] - }), - rollupTypescript({ - tsconfig: runtimeTsconfigPath - }), - ...(minify - ? [ - rollupTerser({ - compress: true, - mangle: true, - format: { - comments: false - } - }) - ] - : []) - ] - } -} - -Promise.all(([ - { - input: createInput(false), - output: { - file: runtimeOut, - format: 'iife', - name: 'emnapi', - exports: 'named' - } - }, - { - input: createInput(false), - output: { - file: path.join(path.dirname(runtimeOut), 'emnapi.js'), - format: 'umd', - name: 'emnapi', - exports: 'named' - } - }, - { - input: createInput(true), - output: { - file: path.join(path.dirname(runtimeOut), 'emnapi.min.js'), - format: 'umd', - name: 'emnapi', - exports: 'named' - } - } -]).map(conf => { - return rollup.rollup(conf.input).then(bundle => bundle.write(conf.output)) -})) diff --git a/script/build.js b/script/build.js deleted file mode 100644 index b788550f..00000000 --- a/script/build.js +++ /dev/null @@ -1,63 +0,0 @@ -const fs = require('fs') -const path = require('path') - -const { runtimeOut } = require('./common.js') - -const libTsconfigPath = path.join(__dirname, '../tsconfig.json') -const libTsconfig = JSON.parse(fs.readFileSync(libTsconfigPath, 'utf8')) - -const libOut = path.join(path.dirname(libTsconfigPath), libTsconfig.compilerOptions.outFile) - -const runtimeCode = fs.readFileSync(runtimeOut, 'utf8') -const libCode = fs.readFileSync(libOut, 'utf8') - -fs.writeFileSync(libOut, - libCode - .replace('__EMNAPI_RUNTIME_REPLACE__', `'${runtimeCode.replace(/\\/g, '\\\\').replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}'`) - .replace('__EMNAPI_RUNTIME_INIT__;', '') - .replace(/(makeDynCall\(.*?\))/g, '{{{ $1 }}}') - /* .replace(/(makeMalloc\(.*?\))/g, '{{{ $1 }}}') */, - 'utf8' -) - -fs.writeFileSync(path.join(path.dirname(libOut), path.basename(libOut, '.js') + '_no_runtime.js'), - libCode - .replace('__EMNAPI_RUNTIME_REPLACE__', '""') - .replace('__EMNAPI_RUNTIME_INIT__;', ` - (function () { - if ('emnapiRuntime' in Module) { - emnapi = Module.emnapiRuntime; - } - if (!emnapi && typeof require === 'function') { - try { - if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { - emnapi = require('@tybys/emnapi/dist/emnapi.min.js'); - } else { - emnapi = require('@tybys/emnapi/dist/emnapi.js'); - } - } catch (_) {} - } - if (!emnapi) { - emnapi = (function () { - let g; - g = (function () { return this })(); - - try { - g = g || new Function('return this')(); - } catch (_) { - if (typeof globalThis !== 'undefined') return globalThis; - if (typeof __webpack_public_path__ === 'undefined') { - if (typeof global !== 'undefined') return global; - } - if (typeof window !== 'undefined') return window; - if (typeof self !== 'undefined') return self; - } - - return g; - })().emnapi; - } - })();`) - .replace(/(makeDynCall\(.*?\))/g, '{{{ $1 }}}') - /* .replace(/(makeMalloc\(.*?\))/g, '{{{ $1 }}}') */, - 'utf8' -) diff --git a/script/common.js b/script/common.js deleted file mode 100644 index 170ad88b..00000000 --- a/script/common.js +++ /dev/null @@ -1,5 +0,0 @@ -const path = require('path') - -const runtimeOut = path.join(__dirname, '../dist/library_napi_runtime.js') - -exports.runtimeOut = runtimeOut diff --git a/script/progress.js b/script/progress.js deleted file mode 100644 index 9c7b4fb1..00000000 --- a/script/progress.js +++ /dev/null @@ -1,7 +0,0 @@ -const fs = require('fs') -const path = require('path') - -const readme = fs.readFileSync(path.join(__dirname, '../README.md'), 'utf8') -const total = readme.match(/-\s\[[x ]\] \S+/g) -const implemented = readme.match(/-\s\[x\] \S+/g) -console.log(implemented.length + ' / ' + total.length + ' [' + Math.floor(100 * implemented.length / total.length) + '%]') diff --git a/script/release.js b/script/release.js index 52ae1875..20f16825 100644 --- a/script/release.js +++ b/script/release.js @@ -10,14 +10,15 @@ fs.mkdirSync(path.join(root, 'lib'), { recursive: true }) fs.mkdirSync(path.join(root, 'include/emnapi'), { recursive: true }) fs.mkdirSync(path.join(root, 'src'), { recursive: true }) -fs.copyFileSync(path.join(__dirname, '../dist/library_napi.js'), path.join(root, 'lib', 'library_napi.js')) -fs.copyFileSync(path.join(__dirname, '../dist/library_napi_no_runtime.js'), path.join(root, 'lib', 'library_napi_no_runtime.js')) -fs.copyFileSync(path.join(__dirname, '../dist/emnapi.js'), path.join(root, 'lib', 'emnapi.js')) -fs.copyFileSync(path.join(__dirname, '../dist/emnapi.min.js'), path.join(root, 'lib', 'emnapi.min.js')) -fs.copyFileSync(path.join(__dirname, '../src/emnapi.c'), path.join(root, 'src', 'emnapi.c')) -fs.readdirSync(path.join(__dirname, '../include')).forEach(item => { +fs.copyFileSync(path.join(__dirname, '../packages/emnapi/dist/library_napi.js'), path.join(root, 'lib', 'library_napi.js')) +fs.copyFileSync(path.join(__dirname, '../packages/emnapi/dist/library_napi_no_runtime.js'), path.join(root, 'lib', 'library_napi_no_runtime.js')) +fs.copyFileSync(path.join(__dirname, '../packages/runtime/dist/emnapi.js'), path.join(root, 'lib', 'emnapi.js')) +fs.copyFileSync(path.join(__dirname, '../packages/runtime/dist/emnapi.min.js'), path.join(root, 'lib', 'emnapi.min.js')) +fs.copyFileSync(path.join(__dirname, '../packages/runtime/dist/emnapi.d.ts'), path.join(root, 'lib', 'emnapi.d.ts')) +fs.copyFileSync(path.join(__dirname, '../packages/emnapi/src/emnapi.c'), path.join(root, 'src', 'emnapi.c')) +fs.readdirSync(path.join(__dirname, '../packages/emnapi/include')).forEach(item => { if (item !== '.' && item !== '..') { - fs.copyFileSync(path.join(__dirname, '../include', item), path.join(root, 'include/emnapi', item)) + fs.copyFileSync(path.join(__dirname, '../packages/emnapi/include', item), path.join(root, 'include/emnapi', item)) } }) diff --git a/test/cgen.config.js b/test/cgen.config.js deleted file mode 100644 index 3daba865..00000000 --- a/test/cgen.config.js +++ /dev/null @@ -1,97 +0,0 @@ -module.exports = function (_options, { isDebug, isEmscripten }) { - const compilerFlags = isEmscripten - ? [ - // ...(isDebug ? ['-sDISABLE_EXCEPTION_CATCHING=0'] : []) - ] - : [] - - const linkerFlags = isEmscripten - ? [ - // "-sEXPORTED_FUNCTIONS=['_malloc','_free']", - '-sALLOW_MEMORY_GROWTH=1', - ...(isDebug ? ['-sSAFE_HEAP=1'/* , '-sDISABLE_EXCEPTION_CATCHING=0' */] : []) - ] - : [] - - const includePaths = isEmscripten - ? ['../include'] - : [`${require('path').join(require('os').homedir(), 'AppData/Local/node-gyp/Cache', process.versions.node, 'include/node')}`, '../node_modules/node-addon-api'] - - const createTarget = (name, sources, needEntry) => ({ - name: name, - type: isEmscripten ? 'exe' : 'node', - sources: [...(needEntry ? (sources.push('../test/entry_point.c'), sources) : sources)], - emwrap: { - exports: ['emnapi'] - }, - includePaths, - libs: ['testcommon', ...(isEmscripten ? ['emnapi'] : [])], - compileOptions: [...compilerFlags], - // eslint-disable-next-line no-template-curly-in-string - linkOptions: [...linkerFlags] - }) - - const createNodeAddonApiTarget = (name, sources) => ({ - name: name, - type: isEmscripten ? 'exe' : 'node', - sources: [...sources], - emwrap: { - exports: ['emnapi'] - }, - includePaths, - libs: [...(isEmscripten ? ['emnapi'] : [])], - defines: ['NAPI_DISABLE_CPP_EXCEPTIONS', 'NODE_ADDON_API_ENABLE_MAYBE'], - compileOptions: [...compilerFlags], - // eslint-disable-next-line no-template-curly-in-string - linkOptions: [...linkerFlags] - }) - - return { - project: 'emnapitest', - dependencies: isEmscripten ? { '..': { EMNAPI_LIB_NO_RUNTIME: 'ON' } } : {}, - targets: [ - { - type: 'lib', - name: 'testcommon', - sources: ['../test/common.c'], - includePaths, - compileOptions: [...compilerFlags], - linkOptions: [...linkerFlags] - }, - createTarget('env', ['../test/env/binding.c']), - createTarget('hello', ['../test/hello/binding.c']), - createTarget('arg', ['../test/arg/binding.c'], true), - createTarget('callback', ['../test/callback/binding.c'], true), - createTarget('objfac', ['../test/objfac/binding.c'], true), - createTarget('fnfac', ['../test/fnfac/binding.c'], true), - createTarget('general', ['../test/general/binding.c'], true), - createTarget('string', ['../test/string/binding.c'], true), - createTarget('property', ['../test/property/binding.c'], true), - createTarget('promise', ['../test/promise/binding.c'], true), - createTarget('object', ['../test/object/test_null.c', '../test/object/test_object.c'], true), - createTarget('objwrap', ['../test/objwrap/myobject.cc'], true), - createTarget('bigint', ['../test/bigint/binding.c'], true), - createTarget('fnwrap', ['../test/fnwrap/myobject.cc', '../test/fnwrap/binding.cc'], true), - createTarget('passwrap', ['../test/passwrap/myobject.cc', '../test/passwrap/binding.cc'], true), - createTarget('array', ['../test/array/binding.c'], true), - createTarget('constructor', ['../test/constructor/binding.c'], true), - createTarget('conversion', ['../test/conversion/test_conversions.c', '../test/conversion/test_null.c'], true), - createTarget('dataview', ['../test/dataview/binding.c'], true), - createTarget('date', ['../test/date/binding.c'], true), - createTarget('error', ['../test/error/binding.c'], true), - createTarget('exception', ['../test/exception/binding.c'], true), - createTarget('ref', ['../test/ref/binding.c'], true), - createTarget('ref_double_free', ['../test/ref_double_free/binding.c'], true), - createTarget('function', ['../test/function/binding.c'], true), - createTarget('scope', ['../test/scope/binding.c'], true), - createTarget('newtarget', ['../test/newtarget/binding.c'], true), - createTarget('number', ['../test/number/binding.c'], true), - createTarget('symbol', ['../test/symbol/binding.c'], true), - createTarget('typedarray', ['../test/typedarray/binding.c'], true), - ...(isEmscripten ? [createTarget('emnapitest', ['../test/emnapitest/binding.c'], true)] : []), - createTarget('version', ['../test/version/binding.c']), - - createNodeAddonApiTarget('n_hello', ['../test/node-addon-api/hello/binding.cc']) - ] - } -} diff --git a/tsconfig.json b/tsconfig.json index 44e9c93a..03491535 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,18 @@ { - "extends": "./tsconfig.base.json", "compilerOptions": { - "outFile": "./dist/library_napi.js", - "importHelpers": false, - "noEmitHelpers": true, + "target": "ES5", + "module": "none", + "noImplicitAny": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noImplicitUseStrict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "strictNullChecks": true, + "strictPropertyInitialization": true, + "strictBindCallApply": true, + "strictFunctionTypes": true, + "noEmit": true, "types": [], "lib": [ "ES5", @@ -14,11 +23,6 @@ ] }, "include": [ - "./dist/library_napi_runtime.d.ts", - "./lib/typings/runtime.d.ts", - "./lib/typings/napi.d.ts", - "./lib/typings/compiler.d.ts", - "./lib/*.ts", - "./lib/value/*.ts" + "./packages/**/*.ts" ] }