|
| 1 | +// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package v8go |
| 6 | + |
| 7 | +// #include <stdlib.h> |
| 8 | +// #include "v8go.h" |
| 9 | +import "C" |
| 10 | +import ( |
| 11 | + "errors" |
| 12 | + "fmt" |
| 13 | + "math/big" |
| 14 | + "unsafe" |
| 15 | +) |
| 16 | + |
| 17 | +// Object is a JavaScript object (ECMA-262, 4.3.3) |
| 18 | +type Object struct { |
| 19 | + *Value |
| 20 | +} |
| 21 | + |
| 22 | +// Set will set a property on the Object to a given value. |
| 23 | +// Supports all value types, eg: Object, Array, Date, Set, Map etc |
| 24 | +// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) |
| 25 | +// then a *Value will be created and set as the value property. |
| 26 | +func (o *Object) Set(key string, val interface{}) error { |
| 27 | + if len(key) == 0 { |
| 28 | + return errors.New("v8go: You must provide a valid property key") |
| 29 | + } |
| 30 | + return set(o, key, 0, val) |
| 31 | +} |
| 32 | + |
| 33 | +// Set will set a given index on the Object to a given value. |
| 34 | +// Supports all value types, eg: Object, Array, Date, Set, Map etc |
| 35 | +// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) |
| 36 | +// then a *Value will be created and set as the value property. |
| 37 | +func (o *Object) SetIdx(idx uint32, val interface{}) error { |
| 38 | + return set(o, "", idx, val) |
| 39 | +} |
| 40 | + |
| 41 | +func set(o *Object, key string, idx uint32, val interface{}) error { |
| 42 | + var value *Value |
| 43 | + switch v := val.(type) { |
| 44 | + case string, int32, uint32, int64, uint64, float64, bool, *big.Int: |
| 45 | + // ignoring error as code cannot reach the error state as we are already |
| 46 | + // validating the new value types in this case statement |
| 47 | + value, _ = NewValue(o.ctx.iso, v) |
| 48 | + case Valuer: |
| 49 | + value = v.value() |
| 50 | + default: |
| 51 | + return fmt.Errorf("v8go: unsupported object property type `%T`", v) |
| 52 | + } |
| 53 | + |
| 54 | + if len(key) > 0 { |
| 55 | + ckey := C.CString(key) |
| 56 | + defer C.free(unsafe.Pointer(ckey)) |
| 57 | + C.ObjectSet(o.ptr, ckey, value.ptr) |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + C.ObjectSetIdx(o.ptr, C.uint32_t(idx), value.ptr) |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// Get tries to get a Value for a given Object property key. |
| 66 | +func (o *Object) Get(key string) (*Value, error) { |
| 67 | + ckey := C.CString(key) |
| 68 | + defer C.free(unsafe.Pointer(ckey)) |
| 69 | + |
| 70 | + rtn := C.ObjectGet(o.ptr, ckey) |
| 71 | + return getValue(o.ctx, rtn), getError(rtn) |
| 72 | +} |
| 73 | + |
| 74 | +// GetIdx tries to get a Value at a give Object index. |
| 75 | +func (o *Object) GetIdx(idx uint32) (*Value, error) { |
| 76 | + rtn := C.ObjectGetIdx(o.ptr, C.uint32_t(idx)) |
| 77 | + return getValue(o.ctx, rtn), getError(rtn) |
| 78 | +} |
| 79 | + |
| 80 | +// Has calls the abstract operation HasProperty(O, P) described in ECMA-262, 7.3.10. |
| 81 | +// Returns true, if the object has the property, either own or on the prototype chain. |
| 82 | +func (o *Object) Has(key string) bool { |
| 83 | + ckey := C.CString(key) |
| 84 | + defer C.free(unsafe.Pointer(ckey)) |
| 85 | + return C.ObjectHas(o.ptr, ckey) != 0 |
| 86 | +} |
| 87 | + |
| 88 | +// HasIdx returns true if the object has a value at the given index. |
| 89 | +func (o *Object) HasIdx(idx uint32) bool { |
| 90 | + return C.ObjectHasIdx(o.ptr, C.uint32_t(idx)) != 0 |
| 91 | +} |
| 92 | + |
| 93 | +// Delete returns true if successful in deleting a named property on the object. |
| 94 | +func (o *Object) Delete(key string) bool { |
| 95 | + ckey := C.CString(key) |
| 96 | + defer C.free(unsafe.Pointer(ckey)) |
| 97 | + return C.ObjectDelete(o.ptr, ckey) != 0 |
| 98 | +} |
| 99 | + |
| 100 | +// DeleteIdx returns true if successful in deleting a value at a given index of the object. |
| 101 | +func (o *Object) DeleteIdx(idx uint32) bool { |
| 102 | + return C.ObjectDeleteIdx(o.ptr, C.uint32_t(idx)) != 0 |
| 103 | +} |
0 commit comments