Skip to content

Foreign Function Interface (FFI) implementation #46905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,31 @@ The externally maintained libraries used by Node.js are:
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""

- libffi, located at deps/libffi, is licensed as follows:
"""
libffi - Copyright (c) 1996-2022 Anthony Green, Red Hat, Inc and others.
See source files for details.

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.
"""

- npm, located at deps/npm, is licensed as follows:
"""
The npm application
Expand Down
50 changes: 44 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ with-code-cache test-code-cache:

out/Makefile: config.gypi common.gypi node.gyp \
deps/uv/uv.gyp deps/llhttp/llhttp.gyp deps/zlib/zlib.gyp \
deps/simdutf/simdutf.gyp deps/ada/ada.gyp \
deps/simdutf/simdutf.gyp deps/ada/ada.gyp deps/libffi/libffi.gyp \
tools/v8_gypfiles/toolchain.gypi tools/v8_gypfiles/features.gypi \
tools/v8_gypfiles/inspector.gypi tools/v8_gypfiles/v8.gyp
$(PYTHON) tools/gyp_node.py -f make
Expand Down Expand Up @@ -417,6 +417,30 @@ test/addons/.buildstamp: $(ADDONS_PREREQS) \
# TODO(bnoordhuis) Force rebuild after gyp update.
build-addons: | $(NODE_EXE) test/addons/.buildstamp

FFI_BINDING_GYPS := \
$(filter-out test/ffi/??_*/binding.gyp, \
$(wildcard test/ffi/*/binding.gyp))

FFI_BINDING_SOURCES := \
$(filter-out test/ffi/??_*/*.c, $(wildcard test/ffi/*/*.c)) \
$(filter-out test/ffi/??_*/*.h, $(wildcard test/ffi/*/*.h))

# Implicitly depends on $(NODE_EXE), see the build-ffi-tests rule for rationale.
# Depends on node-gyp package.json so that build-ffi-tests is (re)executed when
# node-gyp is updated as part of an npm update.
test/ffi/.buildstamp: $(ADDONS_PREREQS) \
$(FFI_BINDING_GYPS) $(FFI_BINDING_SOURCES)
@$(call run_build_addons,"$$PWD/test/ffi",$@)

.PHONY: build-ffi-tests
# .buildstamp needs $(NODE_EXE) but cannot depend on it
# directly because it calls make recursively. The parent make cannot know
# if the subprocess touched anything so it pessimistically assumes that
# .buildstamp is out of date and need a rebuild.
# Just goes to show that recursive make really is harmful...
# TODO(bnoordhuis) Force rebuild after gyp update.
build-ffi-tests: | $(NODE_EXE) test/ffi/.buildstamp

JS_NATIVE_API_BINDING_GYPS := \
$(filter-out test/js-native-api/??_*/binding.gyp, \
$(wildcard test/js-native-api/*/binding.gyp))
Expand Down Expand Up @@ -488,7 +512,10 @@ clear-stalled:
fi

.PHONY: test-build
test-build: | all build-addons build-js-native-api-tests build-node-api-tests
test-build: | all build-addons build-ffi-tests build-js-native-api-tests build-node-api-tests

.PHONY: test-build-ffi
test-build-ffi: all build-ffi-tests

.PHONY: test-build-js-native-api
test-build-js-native-api: all build-js-native-api-tests
Expand All @@ -509,7 +536,7 @@ test-all-suites: | clear-stalled test-build bench-addons-build doc-only ## Run a
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) test/*

JS_SUITES ?= default
NATIVE_SUITES ?= addons js-native-api node-api
NATIVE_SUITES ?= addons ffi js-native-api node-api
# CI_* variables should be kept synchronized with the ones in vcbuild.bat
CI_NATIVE_SUITES ?= $(NATIVE_SUITES) benchmark
CI_JS_SUITES ?= $(JS_SUITES) pummel
Expand All @@ -523,7 +550,7 @@ endif
# Build and test addons without building anything else
# Related CI job: node-test-commit-arm-fanned
test-ci-native: LOGLEVEL := info
test-ci-native: | benchmark/napi/.buildstamp test/addons/.buildstamp test/js-native-api/.buildstamp test/node-api/.buildstamp
test-ci-native: | benchmark/napi/.buildstamp test/addons/.buildstamp test/ffi/.buildstamp test/js-native-api/.buildstamp test/node-api/.buildstamp
$(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \
--mode=$(BUILDTYPE_LOWER) --flaky-tests=$(FLAKY_TESTS) \
$(TEST_CI_ARGS) $(CI_NATIVE_SUITES)
Expand All @@ -545,7 +572,7 @@ test-ci-js: | clear-stalled
.PHONY: test-ci
# Related CI jobs: most CI tests, excluding node-test-commit-arm-fanned
test-ci: LOGLEVEL := info
test-ci: | clear-stalled bench-addons-build build-addons build-js-native-api-tests build-node-api-tests doc-only
test-ci: | clear-stalled bench-addons-build build-addons build-ffi-tests build-js-native-api-tests build-node-api-tests doc-only
out/Release/cctest --gtest_output=xml:out/junit/cctest.xml
$(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \
--mode=$(BUILDTYPE_LOWER) --flaky-tests=$(FLAKY_TESTS) \
Expand Down Expand Up @@ -643,6 +670,16 @@ test-js-native-api-clean:
$(RM) -r test/js-native-api/*/build
$(RM) test/js-native-api/.buildstamp

.PHONY: test-ffi
test-ffi: test-build-ffi
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) ffi

.PHONY: test-ffi-clean
.NOTPARALLEL: test-ffi-clean
test-ffi-clean:
$(RM) -r test/ffi/*/build
$(RM) test/ffi/.buildstamp

.PHONY: test-node-api
test-node-api: test-build-node-api
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) node-api
Expand All @@ -654,7 +691,7 @@ test-node-api-clean:
$(RM) test/node-api/.buildstamp

.PHONY: test-addons
test-addons: test-build test-js-native-api test-node-api
test-addons: test-build test-ffi test-js-native-api test-node-api
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) addons

.PHONY: test-addons-clean
Expand All @@ -663,6 +700,7 @@ test-addons-clean:
$(RM) -r test/addons/??_*/
$(RM) -r test/addons/*/build
$(RM) test/addons/.buildstamp test/addons/.docbuildstamp
$(MAKE) test-ffi-clean
$(MAKE) test-js-native-api-clean
$(MAKE) test-node-api-clean

Expand Down
18 changes: 18 additions & 0 deletions benchmark/ffi/getpid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const common = require('../common.js');
const ffi = require('node:ffi');

const bench = common.createBenchmark(main, {
n: [1e7],
});

const getpid = ffi.getNativeFunction(null, 'uv_os_getpid', 'int', []);

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
getpid();
bench.end(n);

}
17 changes: 17 additions & 0 deletions deps/libffi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.libs
.deps
*.o
*.lo
.dirstamp
*.la
*.pdb
*.obj
Makefile
config.log
config.status
*~
libffi.pc
libtool
stamp-h1
libffi*gz
autom4te.cache
Loading