From 31e6d0aad1ea2e38e9c6093d174280a0319b580b Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 1 Nov 2023 12:39:22 -0700 Subject: [PATCH] In STRICT mode, only define `assert` when ASSERTIONS is set This matches the behaviour of MINIMAL_RUNTIME, and supports the practice of not including asserts in release builds. Also remove the closure externs for `assert` so that closure can correctly error out end `assert` is not defined. Helps with #20504 --- ChangeLog.md | 4 + src/closure-externs/node-externs.js | 5 - src/library_pthread.js | 2 + src/preamble.js | 6 + .../closure-compiler/node-externs/assert.js | 137 ------------------ 5 files changed, 12 insertions(+), 142 deletions(-) delete mode 100644 third_party/closure-compiler/node-externs/assert.js diff --git a/ChangeLog.md b/ChangeLog.md index 522f4746bdf43..c51b463aa0084 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works. 3.1.48 (in development) ----------------------- +- The JS `assert` function is no longer available in release builds when + `-sSTRICT` is used. This should only affect users with custom JS library code + which doesn't use `#if ASSERTIONS` guards around their `assert` calls. This + behaviour matches that of `MINIMAL_RUNTIME`. (#20592) - The minimum version of node required run the compiler was updated from 10.19 to 16.20. This does not effect the node requirements of the generated JavaScript code. (#20551) diff --git a/src/closure-externs/node-externs.js b/src/closure-externs/node-externs.js index b895b99cf0475..483a4783d8d23 100644 --- a/src/closure-externs/node-externs.js +++ b/src/closure-externs/node-externs.js @@ -57,11 +57,6 @@ wss._socket.remoteAddress; */ wss._socket.remotePort; -/** - * @suppress {duplicate} - */ -var assert; - /** * @suppress {duplicate} */ diff --git a/src/library_pthread.js b/src/library_pthread.js index 9ac8f21a4cc68..363de5e372d9b 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -592,7 +592,9 @@ var LibraryPThread = { PThread.outstandingPromises[pthread_ptr].resolve(); } #endif +#if ASSERTIONS assert(worker); +#endif PThread.returnWorkerToPool(worker); }, diff --git a/src/preamble.js b/src/preamble.js index ad0d001507b72..7c534455fd150 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -75,6 +75,11 @@ var ABORT = false; // but only when noExitRuntime is false. var EXITSTATUS; +#if ASSERTIONS || !STRICT +// In STRICT mode, we only define assert() when ASSERTIONS is set. i.e. we +// don't define it at all in release modes. This matches the behaviour of +// MINIMAL_RUNTIME. +// TODO(sbc): Make this the default even without STRICT enabled. /** @type {function(*, string=)} */ function assert(condition, text) { if (!condition) { @@ -88,6 +93,7 @@ function assert(condition, text) { #endif } } +#endif #if ASSERTIONS // We used to include malloc/free by default in the past. Show a helpful error in diff --git a/third_party/closure-compiler/node-externs/assert.js b/third_party/closure-compiler/node-externs/assert.js deleted file mode 100644 index cf39ca61eb2f2..0000000000000 --- a/third_party/closure-compiler/node-externs/assert.js +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2012 The Closure Compiler Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @fileoverview Definitions for node's assert module - * @see http://nodejs.org/api/assert.html - * @see https://github.com/joyent/node/blob/master/lib/assert.js - * @externs - * @author Daniel Wirtz - */ - -/** - BEGIN_NODE_INCLUDE - var assert = require('assert'); - END_NODE_INCLUDE - */ - -/** - * @param {*} value - * @param {string} message - * @throws {assert.AssertionError} - */ -var assert = function(value, message) {}; - -/** - * @param {{message: string, actual: *, expected: *, operator: string}} options - * @constructor - * @extends Error - */ -assert.AssertionError = function(options) {}; - -/** - * @return {string} - */ -assert.AssertionError.prototype.toString = function() {}; - -/** - * @param {*} value - * @param {string=} message - * @throws {assert.AssertionError} - */ -assert.ok = function(value, message) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @param {string} operator - * @throws {assert.AssertionError} - */ -assert.fail = function(actual, expected, message, operator) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @throws {assert.AssertionError} - */ -assert.equal = function(actual, expected, message) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @throws {assert.AssertionError} - */ -assert.notEqual = function(actual, expected, message) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @throws {assert.AssertionError} - */ -assert.deepEqual = function(actual, expected, message) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @throws {assert.AssertionError} - */ -assert.notDeepEqual = function(actual, expected, message) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @throws {assert.AssertionError} - */ -assert.strictEqual = function(actual, expected, message) {}; - -/** - * @param {*} actual - * @param {*} expected - * @param {string} message - * @throws {assert.AssertionError} - */ -assert.notStrictEqual = function(actual, expected, message) {}; - -/** - * @name assert.throws - * @function - * @param {function()} block - * @param {Function|RegExp|function(*)} error - * @param {string=} message - * @throws {assert.AssertionError} - */ -// Error: .\assert.js:120: ERROR - Parse error. missing name after . operator -// assert.throws = function(block, error, message) {}; - -/** - * @param {function()} block - * @param {Function|RegExp|function(*)} error - * @param {string=} message - * @throws {assert.AssertionError} - */ -assert.doesNotThrow = function(block, error, message) {}; - -/** - * @param {*} value - * @throws {assert.AssertionError} - */ -assert.ifError = function(value) {};