Skip to content
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

Value methods to assert value kind #50

Merged
merged 7 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,26 +200,58 @@ void ContextDispose(ContextPtr ptr) {

/********** Value **********/

#define LOCAL_VALUE(ptr) \
m_value* val = static_cast<m_value*>(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> value = val->ptr.Get(iso);

void ValueDispose(ValuePtr ptr) {
delete static_cast<m_value*>(ptr);
delete static_cast<m_value*>(ptr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love to get clangtidy/otherwise in here to standardize the non-go code formatting as well.

Copy link
Owner Author

@rogchap rogchap Dec 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree; I did format the whole file, but I thought that was too much of a change to add to this PR, so I reverted that. (missed this line though).

I'll look to add a clang-format in a future PR, but if you're ok having a larger PR, then I'll just run my default format for now? or leave as-is? @tmc

Copy link
Owner Author

@rogchap rogchap Dec 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#51 I guess it depends which PR you approve first 😉

}

const char* ValueToString(ValuePtr ptr) {
m_value* val = static_cast<m_value*>(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(ptr);
String::Utf8Value utf8(iso, value);

Local<Value> value = val->ptr.Get(iso);
String::Utf8Value utf8(iso, value);

return CopyString(utf8);
return CopyString(utf8);
}

int ValueIsUndefined(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsUndefined() ? 1 : 0;
}

int ValueIsNull(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsNull() ? 1 : 0;
}

int ValueIsNullOrUndefined(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsNullOrUndefined() ? 1 : 0;
}

int ValueIsTrue(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsTrue() ? 1 : 0;
}

int ValueIsFalse(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsFalse() ? 1 : 0;
}

int ValueIsString(ValuePtr ptr) {
LOCAL_VALUE(ptr);

return value->IsString() ? 1 : 0;
}


/********** Version **********/

Expand Down
6 changes: 6 additions & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ extern RtnValue RunScript(ContextPtr ctx_ptr, const char* source, const char* or

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 ValueIsString(ValuePtr ptr);

const char* Version();

Expand Down
278 changes: 278 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,284 @@ 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 {
panic("not implemented")
}

// 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 {
panic("not implemented")
}

// IsFunction returns true if this value is a function.
// This is equivalent to `typeof value === 'function'` in JS.
func (v *Value) IsFunction() bool {
panic("not implemented")
}

// IsObject returns true if this value is an object.
func (v *Value) IsObject() bool {
panic("not implemented")
}

// IsBigInt returns true if this value is a bigint.
// This is equivalent to `typeof value === 'bigint'` in JS.
func (v *Value) IsBigInt() bool {
panic("not implemented")
}

// IsBoolean returns true if this value is boolean.
// This is equivalent to `typeof value === 'boolean'` in JS.
func (v *Value) IsBoolean() bool {
panic("not implemented")
}

// IsNumber returns true if this value is a number.
// This is equivalent to `typeof value === 'number'` in JS.
func (v *Value) IsNumber() bool {
panic("not implemented")
}

// IsExternal returns true if this value is an `External` object.
func (v *Value) IsExternal() bool {
panic("not implemented")
}

// IsInt32 returns true if this value is a 32-bit signed integer.
func (v *Value) IsInt32() bool {
panic("not implemented")
}

// IsUint32 returns true if this value is a 32-bit unsigned integer.
func (v *Value) IsUint32() bool {
panic("not implemented")
}

// IsDate returns true if this value is a `Date`.
func (v *Value) IsDate() bool {
panic("not implemented")
}

// IsArgumentsObject returns true if this value is an Arguments object.
func (v *Value) IsArgumentsObject() bool {
panic("not implemented")
}

// IsBigIntObject returns true if this value is a BigInt object.
func (v *Value) IsBigIntObject() bool {
panic("not implemented")
}

// IsNumberObject returns true if this value is a `Number` object.
func (v *Value) IsNumberObject() bool {
panic("not implemented")
}

// IsStringObject returns true if this value is a `String` object.
func (v *Value) IsStringObject() bool {
panic("not implemented")
}

// IsSymbolObject returns true if this value is a `Symbol` object.
func (v *Value) IsSymbolObject() bool {
panic("not implemented")
}

// IsNativeError returns true if this value is a NativeError.
func (v *Value) IsNativeError() bool {
panic("not implemented")
}

// IsRegExp returns true if this value is a `RegExp`.
func (v *Value) IsRegExp() bool {
panic("not implemented")
}

// IsAsyncFunc returns true if this value is an async function.
func (v *Value) IsAsyncFunc() bool {
panic("not implemented")
}

// Is IsGeneratorFunc returns true if this value is a Generator function.
func (v *Value) IsGeneratorFunc() bool {
panic("not implemented")
}

// IsGeneratorObject returns true if this value is a Generator object (iterator).
func (v *Value) IsGeneratorObject() bool {
panic("not implemented")
}

// IsPromise returns true if this value is a `Promise`.
func (v *Value) IsPromise() bool {
panic("not implemented")
}

// IsMap returns true if this value is a `Map`.
func (v *Value) IsMap() bool {
panic("not implemented")
}

// IsSet returns true if this value is a `Set`.
func (v *Value) IsSet() bool {
panic("not implemented")
}

// IsMapIterator returns true if this value is a `Map` Iterator.
func (v *Value) IsMapIterator() bool {
panic("not implemented")
}

// IsSetIterator returns true if this value is a `Set` Iterator.
func (v *Value) IsSetIterator() bool {
panic("not implemented")
}

// IsWeakMap returns true if this value is a `WeakMap`.
func (v *Value) IsWeakMap() bool {
panic("not implemented")
}

// IsWeakSet returns true if this value is a `WeakSet`.
func (v *Value) IsWeakSet() bool {
panic("not implemented")
}

// IsArrayBuffer returns true if this value is an `ArrayBuffer`.
func (v *Value) IsArrayBuffer() bool {
panic("not implemented")
}

// IsArrayBufferView returns true if this value is an `ArrayBufferView`.
func (v *Value) IsArrayBufferView() bool {
panic("not implemented")
}

// IsTypedArray returns true if this value is one of TypedArrays.
func (v *Value) IsTypedArray() bool {
panic("not implemented")
}

// IsUint8Array returns true if this value is an `Uint8Array`.
func (v *Value) IsUint8Array() bool {
panic("not implemented")
}

// IsUint8ClampedArray returns true if this value is an `Uint8ClampedArray`.
func (v *Value) IsUint8ClampedArray() bool {
panic("not implemented")
}

// IsInt8Array returns true if this value is an `Int8Array`.
func (v *Value) IsInt8Array() bool {
panic("not implemented")
}

// IsUint16Array returns true if this value is an `Uint16Array`.
func (v *Value) IsUint16Array() bool {
panic("not implemented")
}

// IsInt16Array returns true if this value is an `Int16Array`.
func (v *Value) IsInt16Array() bool {
panic("not implemented")
}

// IsUint32Array returns true if this value is an `Uint32Array`.
func (v *Value) IsUint32Array() bool {
panic("not implemented")
}

// IsInt32Array returns true if this value is an `Int32Array`.
func (v *Value) IsInt32Array() bool {
panic("not implemented")
}

// IsFloat32Array returns true if this value is a `Float32Array`.
func (v *Value) IsFloat32Array() bool {
panic("not implemented")
}

// IsFloat64Array returns true if this value is a `Float64Array`.
func (v *Value) IsFloat64Array() bool {
panic("not implemented")
}

// IsBigInt64Array returns true if this value is a `BigInt64Array`.
func (v *Value) IsBigInt64Array() bool {
panic("not implemented")
}

// IsBigUint64Array returns true if this value is a BigUint64Array`.
func (v *Value) IsBigUint64Array() bool {
panic("not implemented")
}

// IsDataView returns true if this value is a `DataView`.
func (v *Value) IsDataView() bool {
panic("not implemented")
}

// IsSharedArrayBuffer returns true if this value is a `SharedArrayBuffer`.
func (v *Value) IsSharedArrayBuffer() bool {
panic("not implemented")
}

// IsProxy returns true if this value is a JavaScript `Proxy`.
func (v *Value) IsProxy() bool {
panic("not implemented")
}

// IsWasmModuleObject returns true if this value is a `WasmModuleObject`.
func (v *Value) IsWasmModuleObject() bool {
panic("not implemented")
}

// IsModuleNamespaceObject returns true if the value is a `Module` Namespace `Object`.
func (v *Value) IsModuleNamespaceObject() bool {
panic("not implemented")
}

func (v *Value) finalizer() {
C.ValueDispose(v.ptr)
v.ptr = nil
Expand Down
Loading