Skip to content

Commit 969b85c

Browse files
bnoordhuisitaloacasas
authored andcommitted
test: remove dependency on node-weak
Replace node-weak with a small hand-rolled add-on. We can now drop node-weak and nan, reducing the size of the source tree by about 750 kB and the size of the tarball by about 150-300 kB. PR-URL: #11239 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 0cded6a commit 969b85c

12 files changed

+115
-43
lines changed

LICENSE

-17
Original file line numberDiff line numberDiff line change
@@ -1071,23 +1071,6 @@ The externally maintained libraries used by Node.js are:
10711071
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10721072
"""
10731073

1074-
- node-weak, located at test/gc/node_modules/weak, is licensed as follows:
1075-
"""
1076-
Copyright (c) 2011, Ben Noordhuis <[email protected]>
1077-
1078-
Permission to use, copy, modify, and/or distribute this software for any
1079-
purpose with or without fee is hereby granted, provided that the above
1080-
copyright notice and this permission notice appear in all copies.
1081-
1082-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1083-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1084-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1085-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1086-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1087-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1088-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1089-
"""
1090-
10911074
- v8_inspector, located at deps/v8_inspector/third_party/v8_inspector, is licensed as follows:
10921075
"""
10931076
// Copyright 2015 The Chromium Authors. All rights reserved.

Makefile

+6-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ test-parallel: all
133133
test-valgrind: all
134134
$(PYTHON) tools/test.py --mode=release --valgrind sequential parallel message
135135

136-
test/gc/node_modules/weak/build/Release/weakref.node: $(NODE_EXE)
136+
test/gc/build/Release/binding.node: \
137+
$(NODE_EXE) test/gc/binding.cc test/gc/binding.gyp
137138
$(NODE) deps/npm/node_modules/node-gyp/bin/node-gyp rebuild \
138139
--python="$(PYTHON)" \
139-
--directory="$(shell pwd)/test/gc/node_modules/weak" \
140+
--directory="$(shell pwd)/test/gc" \
140141
--nodedir="$(shell pwd)"
141142

142143
# Implicitly depends on $(NODE_EXE), see the build-addons rule for rationale.
@@ -197,12 +198,12 @@ clear-stalled:
197198
ps awwx | grep Release/node | grep -v grep | cat
198199
ps awwx | grep Release/node | grep -v grep | awk '{print $$1}' | $(XARGS) kill
199200

200-
test-gc: all test/gc/node_modules/weak/build/Release/weakref.node
201+
test-gc: all test/gc/build/Release/binding.node
201202
$(PYTHON) tools/test.py --mode=release gc
202203

203204
test-build: | all build-addons
204205

205-
test-all: test-build test/gc/node_modules/weak/build/Release/weakref.node
206+
test-all: test-build test/gc/build/Release/binding.node
206207
$(PYTHON) tools/test.py --mode=debug,release
207208

208209
test-all-valgrind: test-build
@@ -737,6 +738,7 @@ CPPLINT_FILES = $(filter-out $(CPPLINT_EXCLUDE), $(wildcard \
737738
test/addons/*/*.h \
738739
test/cctest/*.cc \
739740
test/cctest/*.h \
741+
test/gc/binding.cc \
740742
tools/icu/*.cc \
741743
tools/icu/*.h \
742744
))

test/gc/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

test/gc/binding.cc

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "node.h"
2+
#include "uv.h"
3+
4+
#include <assert.h>
5+
#include <stdlib.h>
6+
7+
#include <vector>
8+
9+
#ifdef NDEBUG
10+
#define CHECK(x) do { if (!(x)) abort(); } while (false)
11+
#else
12+
#define CHECK assert
13+
#endif
14+
15+
#define CHECK_EQ(a, b) CHECK((a) == (b))
16+
17+
namespace {
18+
19+
struct Callback {
20+
inline Callback(v8::Isolate* isolate,
21+
v8::Local<v8::Object> object,
22+
v8::Local<v8::Function> function)
23+
: object(isolate, object), function(isolate, function) {}
24+
25+
v8::Persistent<v8::Object> object;
26+
v8::Persistent<v8::Function> function;
27+
};
28+
29+
static uv_async_t async_handle;
30+
static std::vector<Callback*> callbacks;
31+
32+
inline void Prime() {
33+
uv_ref(reinterpret_cast<uv_handle_t*>(&async_handle));
34+
CHECK_EQ(0, uv_async_send(&async_handle));
35+
}
36+
37+
inline void AsyncCallback(uv_async_t*) {
38+
auto isolate = v8::Isolate::GetCurrent();
39+
v8::HandleScope handle_scope(isolate);
40+
auto context = isolate->GetCurrentContext();
41+
auto global = context->Global();
42+
while (!callbacks.empty()) {
43+
v8::HandleScope handle_scope(isolate);
44+
auto callback = callbacks.back();
45+
callbacks.pop_back();
46+
auto function = v8::Local<v8::Function>::New(isolate, callback->function);
47+
delete callback;
48+
if (node::MakeCallback(isolate, global, function, 0, nullptr).IsEmpty())
49+
return Prime(); // Have exception, flush remainder on next loop tick.
50+
}
51+
uv_unref(reinterpret_cast<uv_handle_t*>(&async_handle));
52+
}
53+
54+
inline void OnGC(const v8::FunctionCallbackInfo<v8::Value>& info) {
55+
CHECK(info[0]->IsObject());
56+
CHECK(info[1]->IsFunction());
57+
auto object = info[0].As<v8::Object>();
58+
auto function = info[1].As<v8::Function>();
59+
auto callback = new Callback(info.GetIsolate(), object, function);
60+
auto on_callback = [] (const v8::WeakCallbackInfo<Callback>& data) {
61+
auto callback = data.GetParameter();
62+
callbacks.push_back(callback);
63+
callback->object.Reset();
64+
Prime();
65+
};
66+
callback->object.SetWeak(callback, on_callback,
67+
v8::WeakCallbackType::kParameter);
68+
}
69+
70+
inline void Initialize(v8::Local<v8::Object> exports,
71+
v8::Local<v8::Value> module,
72+
v8::Local<v8::Context> context) {
73+
NODE_SET_METHOD(module->ToObject(context).ToLocalChecked(), "exports", OnGC);
74+
CHECK_EQ(0, uv_async_init(uv_default_loop(), &async_handle, AsyncCallback));
75+
uv_unref(reinterpret_cast<uv_handle_t*>(&async_handle));
76+
}
77+
78+
} // anonymous namespace
79+
80+
NODE_MODULE_CONTEXT_AWARE(binding, Initialize)

test/gc/binding.gyp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
6+
'sources': [ 'binding.cc' ],
7+
},
8+
],
9+
}

test/gc/test-http-client-connaborted.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// just like test/gc/http-client.js,
33
// but aborting every connection that comes in.
44

5-
require('../common');
5+
const common = require('../common');
66

77
function serverHandler(req, res) {
88
res.connection.destroy();
99
}
1010

1111
const http = require('http');
12-
const weak = require('weak');
12+
const weak = require(`./build/${common.buildType}/binding`);
1313
const todo = 500;
1414
let done = 0;
1515
let count = 0;

test/gc/test-http-client-onerror.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// just like test/gc/http-client.js,
33
// but with an on('error') handler that does nothing.
44

5-
require('../common');
5+
const common = require('../common');
66

77
function serverHandler(req, res) {
88
req.resume();
@@ -11,7 +11,7 @@ function serverHandler(req, res) {
1111
}
1212

1313
const http = require('http');
14-
const weak = require('weak');
14+
const weak = require(`./build/${common.buildType}/binding`);
1515
const todo = 500;
1616
let done = 0;
1717
let count = 0;

test/gc/test-http-client-timeout.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// just like test/gc/http-client.js,
33
// but with a timeout set
44

5-
require('../common');
5+
const common = require('../common');
66

77
function serverHandler(req, res) {
88
setTimeout(function() {
@@ -13,7 +13,7 @@ function serverHandler(req, res) {
1313
}
1414

1515
const http = require('http');
16-
const weak = require('weak');
16+
const weak = require(`./build/${common.buildType}/binding`);
1717
const todo = 550;
1818
let done = 0;
1919
let count = 0;

test/gc/test-http-client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22
// just a simple http server and client.
33

4-
require('../common');
4+
const common = require('../common');
55

66
function serverHandler(req, res) {
77
res.writeHead(200, {'Content-Type': 'text/plain'});
88
res.end('Hello World\n');
99
}
1010

1111
const http = require('http');
12-
const weak = require('weak');
12+
const weak = require(`./build/${common.buildType}/binding`);
1313
const todo = 500;
1414
let done = 0;
1515
let count = 0;

test/gc/test-net-timeout.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// just like test/gc/http-client-timeout.js,
33
// but using a net server/client instead
44

5-
require('../common');
5+
const common = require('../common');
66

77
function serverHandler(sock) {
88
sock.setTimeout(120000);
@@ -19,7 +19,7 @@ function serverHandler(sock) {
1919
}
2020

2121
const net = require('net');
22-
const weak = require('weak');
22+
const weak = require(`./build/${common.buildType}/binding`);
2323
const assert = require('assert');
2424
const todo = 500;
2525
let done = 0;

tools/license-builder.sh

-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ addlicense "cpplint.py" "tools/cpplint.py" \
7979
"$(sed -e '/^$/,$d' -e 's/^#$//' -e 's/^# //' ${rootdir}/tools/cpplint.py | tail -n +3)"
8080
addlicense "ESLint" "tools/eslint" "$(cat ${rootdir}/tools/eslint/LICENSE)"
8181
addlicense "gtest" "deps/gtest" "$(cat ${rootdir}/deps/gtest/LICENSE)"
82-
addlicense "node-weak" "test/gc/node_modules/weak" \
83-
"$(cat ${rootdir}/test/gc/node_modules/weak/LICENSE)"
8482

8583
# v8_inspector
8684
addlicense "v8_inspector" "deps/v8_inspector/third_party/v8_inspector" \

vcbuild.bat

+9-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ set msi=
2727
set upload=
2828
set licensertf=
2929
set jslint=
30-
set buildnodeweak=
30+
set build_testgc_addon=
3131
set noetw=
3232
set noetw_msi_arg=
3333
set noperfctr=
@@ -61,12 +61,12 @@ if /i "%1"=="test-ci" set test_args=%test_args% %test_ci_args% -p tap --lo
6161
if /i "%1"=="test-addons" set test_args=%test_args% addons&set build_addons=1&goto arg-ok
6262
if /i "%1"=="test-simple" set test_args=%test_args% sequential parallel -J&goto arg-ok
6363
if /i "%1"=="test-message" set test_args=%test_args% message&goto arg-ok
64-
if /i "%1"=="test-gc" set test_args=%test_args% gc&set buildnodeweak=1&goto arg-ok
64+
if /i "%1"=="test-gc" set test_args=%test_args% gc&set build_testgc_addon=1&goto arg-ok
6565
if /i "%1"=="test-inspector" set test_args=%test_args% inspector&goto arg-ok
6666
if /i "%1"=="test-tick-processor" set test_args=%test_args% tick-processor&goto arg-ok
6767
if /i "%1"=="test-internet" set test_args=%test_args% internet&goto arg-ok
6868
if /i "%1"=="test-pummel" set test_args=%test_args% pummel&goto arg-ok
69-
if /i "%1"=="test-all" set test_args=%test_args% sequential parallel message gc inspector internet pummel&set buildnodeweak=1&set jslint=1&goto arg-ok
69+
if /i "%1"=="test-all" set test_args=%test_args% sequential parallel message gc inspector internet pummel&set build_testgc_addon=1&set jslint=1&goto arg-ok
7070
if /i "%1"=="test-known-issues" set test_args=%test_args% known_issues&goto arg-ok
7171
if /i "%1"=="jslint" set jslint=1&goto arg-ok
7272
if /i "%1"=="jslint-ci" set jslint_ci=1&goto arg-ok
@@ -295,15 +295,14 @@ ssh -F %SSHCONFIG% %STAGINGSERVER% "touch nodejs/%DISTTYPEDIR%/v%FULLVERSION%/no
295295
:run
296296
@rem Run tests if requested.
297297

298-
:build-node-weak
299-
@rem Build node-weak if required
300-
if "%buildnodeweak%"=="" goto build-addons
301-
"%config%\node" deps\npm\node_modules\node-gyp\bin\node-gyp rebuild --directory="%~dp0test\gc\node_modules\weak" --nodedir="%~dp0."
302-
if errorlevel 1 goto build-node-weak-failed
298+
@rem Build test/gc add-on if required.
299+
if "%build_testgc_addon%"=="" goto build-addons
300+
"%config%\node" deps\npm\node_modules\node-gyp\bin\node-gyp rebuild --directory="%~dp0test\gc" --nodedir="%~dp0."
301+
if errorlevel 1 goto build-testgc-addon-failed
303302
goto build-addons
304303

305-
:build-node-weak-failed
306-
echo Failed to build node-weak.
304+
:build-testgc-addon-failed
305+
echo Failed to build test/gc add-on."
307306
goto exit
308307

309308
:build-addons

0 commit comments

Comments
 (0)