diff --git a/src/bun.js/modules/NodeUtilTypesModule.h b/src/bun.js/modules/NodeUtilTypesModule.h index cc4117701e..c5275be052 100644 --- a/src/bun.js/modules/NodeUtilTypesModule.h +++ b/src/bun.js/modules/NodeUtilTypesModule.h @@ -112,32 +112,6 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionIsNativeError, if (value.isCell()) { if (value.inherits() || value.asCell()->type() == ErrorInstanceType) return JSValue::encode(jsBoolean(true)); - - VM& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - JSObject* object = value.toObject(globalObject); - - // node util.isError relies on toString - // https://github.com/nodejs/node/blob/cf8c6994e0f764af02da4fa70bc5962142181bf3/doc/api/util.md#L2923 - PropertySlot slot(object, PropertySlot::InternalMethodType::VMInquiry, &vm); - if (object->getPropertySlot(globalObject, - vm.propertyNames->toStringTagSymbol, slot)) { - EXCEPTION_ASSERT(!scope.exception()); - if (slot.isValue()) { - JSValue value = slot.getValue(globalObject, vm.propertyNames->toStringTagSymbol); - if (value.isString()) { - String tag = asString(value)->value(globalObject); - if (UNLIKELY(scope.exception())) - scope.clearException(); - if (tag == "Error"_s) - return JSValue::encode(jsBoolean(true)); - } - } - } - - JSValue proto = object->getPrototype(vm, globalObject); - if (proto.isCell() && (proto.inherits() || proto.asCell()->type() == ErrorInstanceType || proto.inherits())) - return JSValue::encode(jsBoolean(true)); } return JSValue::encode(jsBoolean(false)); diff --git a/test/js/node/test/parallel/test-util.js b/test/js/node/test/parallel/test-util.js new file mode 100644 index 0000000000..568ecec769 --- /dev/null +++ b/test/js/node/test/parallel/test-util.js @@ -0,0 +1,88 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// 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. + +'use strict'; +// Flags: --expose-internals +const common = require('../common'); +const assert = require('assert'); +const util = require('util'); +const context = require('vm').runInNewContext; + +// isArray +assert.strictEqual(util.isArray([]), true); +assert.strictEqual(util.isArray(Array()), true); +assert.strictEqual(util.isArray(new Array()), true); +assert.strictEqual(util.isArray(new Array(5)), true); +assert.strictEqual(util.isArray(new Array('with', 'some', 'entries')), true); +assert.strictEqual(util.isArray(context('Array')()), true); +assert.strictEqual(util.isArray({}), false); +assert.strictEqual(util.isArray({ push: function() {} }), false); +assert.strictEqual(util.isArray(/regexp/), false); +assert.strictEqual(util.isArray(new Error()), false); +assert.strictEqual(util.isArray({ __proto__: Array.prototype }), false); + +// _extend +assert.deepStrictEqual(util._extend({ a: 1 }), { a: 1 }); +assert.deepStrictEqual(util._extend({ a: 1 }, []), { a: 1 }); +assert.deepStrictEqual(util._extend({ a: 1 }, null), { a: 1 }); +assert.deepStrictEqual(util._extend({ a: 1 }, true), { a: 1 }); +assert.deepStrictEqual(util._extend({ a: 1 }, false), { a: 1 }); +assert.deepStrictEqual(util._extend({ a: 1 }, { b: 2 }), { a: 1, b: 2 }); +assert.deepStrictEqual(util._extend({ a: 1, b: 2 }, { b: 3 }), { a: 1, b: 3 }); + +assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd'); + +{ + assert.strictEqual(util.types.isNativeError(new Error()), true); + assert.strictEqual(util.types.isNativeError(new TypeError()), true); + assert.strictEqual(util.types.isNativeError(new SyntaxError()), true); + assert.strictEqual(util.types.isNativeError(new (context('Error'))()), true); + assert.strictEqual( + util.types.isNativeError(new (context('TypeError'))()), + true + ); + assert.strictEqual( + util.types.isNativeError(new (context('SyntaxError'))()), + true + ); + assert.strictEqual(util.types.isNativeError({}), false); + assert.strictEqual( + util.types.isNativeError({ name: 'Error', message: '' }), + false + ); + assert.strictEqual(util.types.isNativeError([]), false); + assert.strictEqual( + util.types.isNativeError({ __proto__: Error.prototype }), + false + ); + // assert.strictEqual( + // util.types.isNativeError(new errors.codes.ERR_IPC_CHANNEL_CLOSED()), + // true + // ); +} + +assert.throws(() => { + util.stripVTControlCharacters({}); +}, { + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "str" argument must be of type string.' + + common.invalidArgTypeHelper({}) +});