From 00a5f9ed27be3d892271fd430b2d466f5fe3b5d3 Mon Sep 17 00:00:00 2001 From: Roger Chapman Date: Mon, 21 Dec 2020 20:24:15 +1100 Subject: [PATCH] Value methods to assert value kind (#50) * Value methods to assert value kind * add other value is methods * add to changelog * fix CI to only trigger one set of test runs on a PR * simplify the C++ code * remove whitespace EOL --- .github/workflows/test.yml | 7 +- CHANGELOG.md | 1 + v8go.cc | 290 +++++++++++++++++++++++++++++++++++-- v8go.h | 54 +++++++ value.go | 287 ++++++++++++++++++++++++++++++++++++ value_test.go | 116 +++++++++++++++ 6 files changed, 744 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 71866e99..20fb0615 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,11 @@ name: CI -on: [push, pull_request, workflow_dispatch] +on: + push: + branches: + - master + pull_request: + workflow_dispatch: jobs: test: diff --git a/CHANGELOG.md b/CHANGELOG.md index 003f9669..63effdab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Value methods for checking value kind (is string, number, array etc) - C formatting via `clang-format` to aid future development ## [v0.3.0] diff --git a/v8go.cc b/v8go.cc index d97c626a..49c71934 100644 --- a/v8go.cc +++ b/v8go.cc @@ -202,24 +202,294 @@ void ContextDispose(ContextPtr ptr) { /********** Value **********/ +#define LOCAL_VALUE(ptr) \ + m_value* val = static_cast(ptr); \ + m_ctx* ctx = val->ctx_ptr; \ + Isolate* iso = ctx->iso; \ + Locker locker(iso); \ + Isolate::Scope isolate_scope(iso); \ + HandleScope handle_scope(iso); \ + Context::Scope context_scope(ctx->ptr.Get(iso)); \ + Local value = val->ptr.Get(iso); + void ValueDispose(ValuePtr ptr) { delete static_cast(ptr); } const char* ValueToString(ValuePtr ptr) { - m_value* val = static_cast(ptr); - m_ctx* ctx = val->ctx_ptr; - Isolate* iso = ctx->iso; + LOCAL_VALUE(ptr); + String::Utf8Value utf8(iso, value); + return CopyString(utf8); +} - Locker locker(iso); - Isolate::Scope isolate_scope(iso); - HandleScope handle_scope(iso); - Context::Scope context_scope(ctx->ptr.Get(iso)); +int ValueIsUndefined(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsUndefined(); +} - Local value = val->ptr.Get(iso); - String::Utf8Value utf8(iso, value); +int ValueIsNull(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsNull(); +} - return CopyString(utf8); +int ValueIsNullOrUndefined(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsNullOrUndefined(); +} + +int ValueIsTrue(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsTrue(); +} + +int ValueIsFalse(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsFalse(); +} + +int ValueIsName(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsName(); +} + +int ValueIsString(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsString(); +} + +int ValueIsSymbol(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsSymbol(); +} + +int ValueIsFunction(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsFunction(); +} + +int ValueIsObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsObject(); +} + +int ValueIsBigInt(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsBigInt(); +} + +int ValueIsBoolean(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsBoolean(); +} + +int ValueIsNumber(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsNumber(); +} + +int ValueIsExternal(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsExternal(); +} + +int ValueIsInt32(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsInt32(); +} + +int ValueIsUint32(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsUint32(); +} + +int ValueIsDate(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsDate(); +} + +int ValueIsArgumentsObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsArgumentsObject(); +} + +int ValueIsBigIntObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsBigIntObject(); +} + +int ValueIsNumberObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsNumberObject(); +} + +int ValueIsStringObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsStringObject(); +} + +int ValueIsSymbolObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsSymbolObject(); +} + +int ValueIsNativeError(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsNativeError(); +} + +int ValueIsRegExp(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsRegExp(); +} + +int ValueIsAsyncFunction(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsAsyncFunction(); +} + +int ValueIsGeneratorFunction(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsGeneratorFunction(); +} + +int ValueIsGeneratorObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsGeneratorObject(); +} + +int ValueIsPromise(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsPromise(); +} + +int ValueIsMap(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsMap(); +} + +int ValueIsSet(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsSet(); +} + +int ValueIsMapIterator(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsMapIterator(); +} + +int ValueIsSetIterator(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsSetIterator(); +} + +int ValueIsWeakMap(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsWeakMap(); +} + +int ValueIsWeakSet(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsWeakSet(); +} + +int ValueIsArray(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsArray(); +} + +int ValueIsArrayBuffer(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsArrayBuffer(); +} + +int ValueIsArrayBufferView(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsArrayBufferView(); +} + +int ValueIsTypedArray(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsTypedArray(); +} + +int ValueIsUint8Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsUint8Array(); +} + +int ValueIsUint8ClampedArray(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsUint8ClampedArray(); +} + +int ValueIsInt8Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsInt8Array(); +} + +int ValueIsUint16Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsUint16Array(); +} + +int ValueIsInt16Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsInt16Array(); +} + +int ValueIsUint32Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsUint32Array(); +} + +int ValueIsInt32Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsInt32Array(); +} + +int ValueIsFloat32Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsFloat32Array(); +} + +int ValueIsFloat64Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsFloat64Array(); +} + +int ValueIsBigInt64Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsBigInt64Array(); +} + +int ValueIsBigUint64Array(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsBigUint64Array(); +} + +int ValueIsDataView(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsDataView(); +} + +int ValueIsSharedArrayBuffer(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsSharedArrayBuffer(); +} + +int ValueIsProxy(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsProxy(); +} + +int ValueIsWasmModuleObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsWasmModuleObject(); +} + +int ValueIsModuleNamespaceObject(ValuePtr ptr) { + LOCAL_VALUE(ptr); + return value->IsModuleNamespaceObject(); } /********** Version **********/ diff --git a/v8go.h b/v8go.h index 8962edb2..209d9a53 100644 --- a/v8go.h +++ b/v8go.h @@ -49,6 +49,60 @@ extern RtnValue RunScript(ContextPtr ctx_ptr, extern void ValueDispose(ValuePtr ptr); const char* ValueToString(ValuePtr ptr); +int ValueIsUndefined(ValuePtr ptr); +int ValueIsNull(ValuePtr ptr); +int ValueIsNullOrUndefined(ValuePtr ptr); +int ValueIsTrue(ValuePtr ptr); +int ValueIsFalse(ValuePtr ptr); +int ValueIsName(ValuePtr ptr); +int ValueIsString(ValuePtr ptr); +int ValueIsSymbol(ValuePtr ptr); +int ValueIsFunction(ValuePtr ptr); +int ValueIsObject(ValuePtr ptr); +int ValueIsBigInt(ValuePtr ptr); +int ValueIsBoolean(ValuePtr ptr); +int ValueIsNumber(ValuePtr ptr); +int ValueIsExternal(ValuePtr ptr); +int ValueIsInt32(ValuePtr ptr); +int ValueIsUint32(ValuePtr ptr); +int ValueIsDate(ValuePtr ptr); +int ValueIsArgumentsObject(ValuePtr ptr); +int ValueIsBigIntObject(ValuePtr ptr); +int ValueIsNumberObject(ValuePtr ptr); +int ValueIsStringObject(ValuePtr ptr); +int ValueIsSymbolObject(ValuePtr ptr); +int ValueIsNativeError(ValuePtr ptr); +int ValueIsRegExp(ValuePtr ptr); +int ValueIsAsyncFunction(ValuePtr ptr); +int ValueIsGeneratorFunction(ValuePtr ptr); +int ValueIsGeneratorObject(ValuePtr ptr); +int ValueIsPromise(ValuePtr ptr); +int ValueIsMap(ValuePtr ptr); +int ValueIsSet(ValuePtr ptr); +int ValueIsMapIterator(ValuePtr ptr); +int ValueIsSetIterator(ValuePtr ptr); +int ValueIsWeakMap(ValuePtr ptr); +int ValueIsWeakSet(ValuePtr ptr); +int ValueIsArray(ValuePtr ptr); +int ValueIsArrayBuffer(ValuePtr ptr); +int ValueIsArrayBufferView(ValuePtr ptr); +int ValueIsTypedArray(ValuePtr ptr); +int ValueIsUint8Array(ValuePtr ptr); +int ValueIsUint8ClampedArray(ValuePtr ptr); +int ValueIsInt8Array(ValuePtr ptr); +int ValueIsUint16Array(ValuePtr ptr); +int ValueIsInt16Array(ValuePtr ptr); +int ValueIsUint32Array(ValuePtr ptr); +int ValueIsInt32Array(ValuePtr ptr); +int ValueIsFloat32Array(ValuePtr ptr); +int ValueIsFloat64Array(ValuePtr ptr); +int ValueIsBigInt64Array(ValuePtr ptr); +int ValueIsBigUint64Array(ValuePtr ptr); +int ValueIsDataView(ValuePtr ptr); +int ValueIsSharedArrayBuffer(ValuePtr ptr); +int ValueIsProxy(ValuePtr ptr); +int ValueIsWasmModuleObject(ValuePtr ptr); +int ValueIsModuleNamespaceObject(ValuePtr ptr); const char* Version(); diff --git a/value.go b/value.go index 5130821e..ec91e5bc 100644 --- a/value.go +++ b/value.go @@ -22,6 +22,293 @@ func (v *Value) String() string { return C.GoString(s) } +// IsUndefined returns true if this value is the undefined value. See ECMA-262 4.3.10. +func (v *Value) IsUndefined() bool { + return C.ValueIsUndefined(v.ptr) != 0 +} + +// IsNull returns true if this value is the null value. See ECMA-262 4.3.11. +func (v *Value) IsNull() bool { + return C.ValueIsNull(v.ptr) != 0 +} + +// IsNullOrUndefined returns true if this value is either the null or the undefined value. +// See ECMA-262 4.3.11. and 4.3.12 +// This is equivalent to `value == null` in JS. +func (v *Value) IsNullOrUndefined() bool { + return C.ValueIsNullOrUndefined(v.ptr) != 0 +} + +// IsTrue returns true if this value is true. +// This is not the same as `BooleanValue()`. The latter performs a conversion to boolean, +// i.e. the result of `Boolean(value)` in JS, whereas this checks `value === true`. +func (v *Value) IsTrue() bool { + return C.ValueIsTrue(v.ptr) != 0 +} + +// IsFalse returns true if this value is false. +// This is not the same as `!BooleanValue()`. The latter performs a conversion to boolean, +// i.e. the result of `!Boolean(value)` in JS, whereas this checks `value === false`. +func (v *Value) IsFalse() bool { + return C.ValueIsFalse(v.ptr) != 0 +} + +// IsName returns true if this value is a symbol or a string. +// This is equivalent to `typeof value === 'string' || typeof value === 'symbol'` in JS. +func (v *Value) IsName() bool { + return C.ValueIsName(v.ptr) != 0 +} + +// IsString returns true if this value is an instance of the String type. See ECMA-262 8.4. +// This is equivalent to `typeof value === 'string'` in JS. +func (v *Value) IsString() bool { + return C.ValueIsString(v.ptr) != 0 +} + +// IsSymbol returns true if this value is a symbol. +// This is equivalent to `typeof value === 'symbol'` in JS. +func (v *Value) IsSymbol() bool { + return C.ValueIsSymbol(v.ptr) != 0 +} + +// IsFunction returns true if this value is a function. +// This is equivalent to `typeof value === 'function'` in JS. +func (v *Value) IsFunction() bool { + return C.ValueIsFunction(v.ptr) != 0 +} + +// IsObject returns true if this value is an object. +func (v *Value) IsObject() bool { + return C.ValueIsObject(v.ptr) != 0 +} + +// IsBigInt returns true if this value is a bigint. +// This is equivalent to `typeof value === 'bigint'` in JS. +func (v *Value) IsBigInt() bool { + return C.ValueIsBigInt(v.ptr) != 0 +} + +// IsBoolean returns true if this value is boolean. +// This is equivalent to `typeof value === 'boolean'` in JS. +func (v *Value) IsBoolean() bool { + return C.ValueIsBoolean(v.ptr) != 0 +} + +// IsNumber returns true if this value is a number. +// This is equivalent to `typeof value === 'number'` in JS. +func (v *Value) IsNumber() bool { + return C.ValueIsNumber(v.ptr) != 0 +} + +// IsExternal returns true if this value is an `External` object. +func (v *Value) IsExternal() bool { + // TODO(rogchap): requires test case + return C.ValueIsExternal(v.ptr) != 0 +} + +// IsInt32 returns true if this value is a 32-bit signed integer. +func (v *Value) IsInt32() bool { + return C.ValueIsInt32(v.ptr) != 0 +} + +// IsUint32 returns true if this value is a 32-bit unsigned integer. +func (v *Value) IsUint32() bool { + return C.ValueIsUint32(v.ptr) != 0 +} + +// IsDate returns true if this value is a `Date`. +func (v *Value) IsDate() bool { + return C.ValueIsDate(v.ptr) != 0 +} + +// IsArgumentsObject returns true if this value is an Arguments object. +func (v *Value) IsArgumentsObject() bool { + return C.ValueIsArgumentsObject(v.ptr) != 0 +} + +// IsBigIntObject returns true if this value is a BigInt object. +func (v *Value) IsBigIntObject() bool { + return C.ValueIsBigIntObject(v.ptr) != 0 +} + +// IsNumberObject returns true if this value is a `Number` object. +func (v *Value) IsNumberObject() bool { + return C.ValueIsNumberObject(v.ptr) != 0 +} + +// IsStringObject returns true if this value is a `String` object. +func (v *Value) IsStringObject() bool { + return C.ValueIsStringObject(v.ptr) != 0 +} + +// IsSymbolObject returns true if this value is a `Symbol` object. +func (v *Value) IsSymbolObject() bool { + return C.ValueIsSymbolObject(v.ptr) != 0 +} + +// IsNativeError returns true if this value is a NativeError. +func (v *Value) IsNativeError() bool { + return C.ValueIsNativeError(v.ptr) != 0 +} + +// IsRegExp returns true if this value is a `RegExp`. +func (v *Value) IsRegExp() bool { + return C.ValueIsRegExp(v.ptr) != 0 +} + +// IsAsyncFunc returns true if this value is an async function. +func (v *Value) IsAsyncFunction() bool { + return C.ValueIsAsyncFunction(v.ptr) != 0 +} + +// Is IsGeneratorFunc returns true if this value is a Generator function. +func (v *Value) IsGeneratorFunction() bool { + return C.ValueIsGeneratorFunction(v.ptr) != 0 +} + +// IsGeneratorObject returns true if this value is a Generator object (iterator). +func (v *Value) IsGeneratorObject() bool { + return C.ValueIsGeneratorObject(v.ptr) != 0 +} + +// IsPromise returns true if this value is a `Promise`. +func (v *Value) IsPromise() bool { + return C.ValueIsPromise(v.ptr) != 0 +} + +// IsMap returns true if this value is a `Map`. +func (v *Value) IsMap() bool { + return C.ValueIsMap(v.ptr) != 0 +} + +// IsSet returns true if this value is a `Set`. +func (v *Value) IsSet() bool { + return C.ValueIsSet(v.ptr) != 0 +} + +// IsMapIterator returns true if this value is a `Map` Iterator. +func (v *Value) IsMapIterator() bool { + return C.ValueIsMapIterator(v.ptr) != 0 +} + +// IsSetIterator returns true if this value is a `Set` Iterator. +func (v *Value) IsSetIterator() bool { + return C.ValueIsSetIterator(v.ptr) != 0 +} + +// IsWeakMap returns true if this value is a `WeakMap`. +func (v *Value) IsWeakMap() bool { + return C.ValueIsWeakMap(v.ptr) != 0 +} + +// IsWeakSet returns true if this value is a `WeakSet`. +func (v *Value) IsWeakSet() bool { + return C.ValueIsWeakSet(v.ptr) != 0 +} + +// IsArray returns true if this value is an array. +// Note that it will return false for a `Proxy` of an array. +func (v *Value) IsArray() bool { + return C.ValueIsArray(v.ptr) != 0 +} + +// IsArrayBuffer returns true if this value is an `ArrayBuffer`. +func (v *Value) IsArrayBuffer() bool { + return C.ValueIsArrayBuffer(v.ptr) != 0 +} + +// IsArrayBufferView returns true if this value is an `ArrayBufferView`. +func (v *Value) IsArrayBufferView() bool { + return C.ValueIsArrayBufferView(v.ptr) != 0 +} + +// IsTypedArray returns true if this value is one of TypedArrays. +func (v *Value) IsTypedArray() bool { + return C.ValueIsTypedArray(v.ptr) != 0 +} + +// IsUint8Array returns true if this value is an `Uint8Array`. +func (v *Value) IsUint8Array() bool { + return C.ValueIsUint8Array(v.ptr) != 0 +} + +// IsUint8ClampedArray returns true if this value is an `Uint8ClampedArray`. +func (v *Value) IsUint8ClampedArray() bool { + return C.ValueIsUint8ClampedArray(v.ptr) != 0 +} + +// IsInt8Array returns true if this value is an `Int8Array`. +func (v *Value) IsInt8Array() bool { + return C.ValueIsInt8Array(v.ptr) != 0 +} + +// IsUint16Array returns true if this value is an `Uint16Array`. +func (v *Value) IsUint16Array() bool { + return C.ValueIsUint16Array(v.ptr) != 0 +} + +// IsInt16Array returns true if this value is an `Int16Array`. +func (v *Value) IsInt16Array() bool { + return C.ValueIsInt16Array(v.ptr) != 0 +} + +// IsUint32Array returns true if this value is an `Uint32Array`. +func (v *Value) IsUint32Array() bool { + return C.ValueIsUint32Array(v.ptr) != 0 +} + +// IsInt32Array returns true if this value is an `Int32Array`. +func (v *Value) IsInt32Array() bool { + return C.ValueIsInt32Array(v.ptr) != 0 +} + +// IsFloat32Array returns true if this value is a `Float32Array`. +func (v *Value) IsFloat32Array() bool { + return C.ValueIsFloat32Array(v.ptr) != 0 +} + +// IsFloat64Array returns true if this value is a `Float64Array`. +func (v *Value) IsFloat64Array() bool { + return C.ValueIsFloat64Array(v.ptr) != 0 +} + +// IsBigInt64Array returns true if this value is a `BigInt64Array`. +func (v *Value) IsBigInt64Array() bool { + return C.ValueIsBigInt64Array(v.ptr) != 0 +} + +// IsBigUint64Array returns true if this value is a BigUint64Array`. +func (v *Value) IsBigUint64Array() bool { + return C.ValueIsBigUint64Array(v.ptr) != 0 +} + +// IsDataView returns true if this value is a `DataView`. +func (v *Value) IsDataView() bool { + return C.ValueIsDataView(v.ptr) != 0 +} + +// IsSharedArrayBuffer returns true if this value is a `SharedArrayBuffer`. +func (v *Value) IsSharedArrayBuffer() bool { + return C.ValueIsSharedArrayBuffer(v.ptr) != 0 +} + +// IsProxy returns true if this value is a JavaScript `Proxy`. +func (v *Value) IsProxy() bool { + return C.ValueIsProxy(v.ptr) != 0 +} + +// IsWasmModuleObject returns true if this value is a `WasmModuleObject`. +func (v *Value) IsWasmModuleObject() bool { + // TODO(rogchap): requires test case + return C.ValueIsWasmModuleObject(v.ptr) != 0 +} + +// IsModuleNamespaceObject returns true if the value is a `Module` Namespace `Object`. +func (v *Value) IsModuleNamespaceObject() bool { + // TODO(rogchap): requires test case + return C.ValueIsModuleNamespaceObject(v.ptr) != 0 +} + func (v *Value) finalizer() { C.ValueDispose(v.ptr) v.ptr = nil diff --git a/value_test.go b/value_test.go index 59c099ef..d9fc3b1d 100644 --- a/value_test.go +++ b/value_test.go @@ -1,6 +1,8 @@ package v8go_test import ( + "reflect" + "runtime" "testing" "rogchap.com/v8go" @@ -32,3 +34,117 @@ func TestValueString(t *testing.T) { }) } } + +func TestValueIsXXX(t *testing.T) { + t.Parallel() + iso, _ := v8go.NewIsolate() + var tests = []struct { + source string + assert func(*v8go.Value) bool + }{ + {"", (*v8go.Value).IsUndefined}, + {"let v; v", (*v8go.Value).IsUndefined}, + {"null", (*v8go.Value).IsNull}, + {"let v; v", (*v8go.Value).IsNullOrUndefined}, + {"let v = null; v", (*v8go.Value).IsNullOrUndefined}, + {"true", (*v8go.Value).IsTrue}, + {"false", (*v8go.Value).IsFalse}, + {"'name'", (*v8go.Value).IsName}, + {"Symbol()", (*v8go.Value).IsName}, + {`"double quote"`, (*v8go.Value).IsString}, + {"'single quote'", (*v8go.Value).IsString}, + {"`string literal`", (*v8go.Value).IsString}, + {"Symbol()", (*v8go.Value).IsSymbol}, + {"Symbol('foo')", (*v8go.Value).IsSymbol}, + {"() => {}", (*v8go.Value).IsFunction}, + {"function v() {}; v", (*v8go.Value).IsFunction}, + {"const v = function() {}; v", (*v8go.Value).IsFunction}, + {"console.log", (*v8go.Value).IsFunction}, + {"Object", (*v8go.Value).IsFunction}, + {"class Foo {}; Foo", (*v8go.Value).IsFunction}, + {"class Foo { bar() {} }; (new Foo()).bar", (*v8go.Value).IsFunction}, + {"function* v(){}; v", (*v8go.Value).IsFunction}, + {"async function v(){}; v", (*v8go.Value).IsFunction}, + {"Object()", (*v8go.Value).IsObject}, + {"new Object", (*v8go.Value).IsObject}, + {"var v = {}; v", (*v8go.Value).IsObject}, + {"10n", (*v8go.Value).IsBigInt}, + {"BigInt(1)", (*v8go.Value).IsBigInt}, + {"true", (*v8go.Value).IsBoolean}, + {"false", (*v8go.Value).IsBoolean}, + {"Boolean()", (*v8go.Value).IsBoolean}, + {"(new Boolean).valueOf()", (*v8go.Value).IsBoolean}, + {"1", (*v8go.Value).IsNumber}, + {"1.1", (*v8go.Value).IsNumber}, + {"1_1", (*v8go.Value).IsNumber}, + {".1", (*v8go.Value).IsNumber}, + {"2e4", (*v8go.Value).IsNumber}, + {"0x2", (*v8go.Value).IsNumber}, + {"NaN", (*v8go.Value).IsNumber}, + {"Infinity", (*v8go.Value).IsNumber}, + {"Number(1)", (*v8go.Value).IsNumber}, + {"(new Number()).valueOf()", (*v8go.Value).IsNumber}, + {"1", (*v8go.Value).IsInt32}, + {"-1", (*v8go.Value).IsInt32}, + {"1", (*v8go.Value).IsUint32}, + {"new Date", (*v8go.Value).IsDate}, + {"function foo(){ return arguments }; foo()", (*v8go.Value).IsArgumentsObject}, + {"Object(1n)", (*v8go.Value).IsBigIntObject}, + {"Object(1)", (*v8go.Value).IsNumberObject}, + {"new Number", (*v8go.Value).IsNumberObject}, + {"new String", (*v8go.Value).IsStringObject}, + {"Object('')", (*v8go.Value).IsStringObject}, + {"Object(Symbol())", (*v8go.Value).IsSymbolObject}, + {"Error()", (*v8go.Value).IsNativeError}, + {"TypeError()", (*v8go.Value).IsNativeError}, + {"SyntaxError()", (*v8go.Value).IsNativeError}, + {"/./", (*v8go.Value).IsRegExp}, + {"RegExp()", (*v8go.Value).IsRegExp}, + {"async function v(){}; v", (*v8go.Value).IsAsyncFunction}, + {"let v = async () => {}; v", (*v8go.Value).IsAsyncFunction}, + {"function* v(){}; v", (*v8go.Value).IsGeneratorFunction}, + {"function* v(){}; v()", (*v8go.Value).IsGeneratorObject}, + {"new Promise(()=>{})", (*v8go.Value).IsPromise}, + {"new Map", (*v8go.Value).IsMap}, + {"new Set", (*v8go.Value).IsSet}, + {"(new Map).entries()", (*v8go.Value).IsMapIterator}, + {"(new Set).entries()", (*v8go.Value).IsSetIterator}, + {"new WeakMap", (*v8go.Value).IsWeakMap}, + {"new WeakSet", (*v8go.Value).IsWeakSet}, + {"new Array", (*v8go.Value).IsArray}, + {"Array()", (*v8go.Value).IsArray}, + {"[]", (*v8go.Value).IsArray}, + {"new ArrayBuffer", (*v8go.Value).IsArrayBuffer}, + {"new Int8Array", (*v8go.Value).IsArrayBufferView}, + {"new Int8Array", (*v8go.Value).IsTypedArray}, + {"new Uint32Array", (*v8go.Value).IsTypedArray}, + {"new Uint8Array", (*v8go.Value).IsUint8Array}, + {"new Uint8ClampedArray", (*v8go.Value).IsUint8ClampedArray}, + {"new Int8Array", (*v8go.Value).IsInt8Array}, + {"new Uint16Array", (*v8go.Value).IsUint16Array}, + {"new Int16Array", (*v8go.Value).IsInt16Array}, + {"new Uint32Array", (*v8go.Value).IsUint32Array}, + {"new Int32Array", (*v8go.Value).IsInt32Array}, + {"new Float32Array", (*v8go.Value).IsFloat32Array}, + {"new Float64Array", (*v8go.Value).IsFloat64Array}, + {"new BigInt64Array", (*v8go.Value).IsBigInt64Array}, + {"new BigUint64Array", (*v8go.Value).IsBigUint64Array}, + {"new DataView(new ArrayBuffer)", (*v8go.Value).IsDataView}, + {"new SharedArrayBuffer", (*v8go.Value).IsSharedArrayBuffer}, + {"new Proxy({},{})", (*v8go.Value).IsProxy}, + } + for _, tt := range tests { + tt := tt + t.Run(tt.source, func(t *testing.T) { + t.Parallel() + ctx, _ := v8go.NewContext(iso) + val, err := ctx.RunScript(tt.source, "test.js") + if err != nil { + t.Fatalf("failed to run script: %v", err) + } + if !tt.assert(val) { + t.Errorf("value is false for %s", runtime.FuncForPC(reflect.ValueOf(tt.assert).Pointer()).Name()) + } + }) + } +}