forked from tommie/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.go
60 lines (51 loc) · 2.1 KB
/
symbol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go
import (
"fmt"
"unsafe"
// #include <stdlib.h>
// #include "v8go.h"
"C"
)
// A Symbol represents a JavaScript symbol (ECMA-262 edition 6).
type Symbol struct {
*Value
}
func SymbolAsyncIterator(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_ASYNC_ITERATOR) }
func SymbolHasInstance(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_HAS_INSTANCE) }
func SymbolIsConcatSpreadable(iso *Isolate) *Symbol {
return symbolByIndex(iso, C.SYMBOL_IS_CONCAT_SPREADABLE)
}
func SymbolIterator(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_ITERATOR) }
func SymbolMatch(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_MATCH) }
func SymbolReplace(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_REPLACE) }
func SymbolSearch(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_SEARCH) }
func SymbolSplit(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_SPLIT) }
func SymbolToPrimitive(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_TO_PRIMITIVE) }
func SymbolToStringTag(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_TO_STRING_TAG) }
func SymbolUnscopables(iso *Isolate) *Symbol { return symbolByIndex(iso, C.SYMBOL_UNSCOPABLES) }
// symbolByIndex is a Go-to-C helper for obtaining builtin symbols.
func symbolByIndex(iso *Isolate, idx C.SymbolIndex) *Symbol {
val := C.BuiltinSymbol(iso.ptr, idx)
if val == nil {
panic(fmt.Errorf("unknown symbol index: %d", idx))
}
return &Symbol{&Value{val, nil}}
}
// Description returns the string representation of the symbol,
// e.g. "Symbol.asyncIterator".
func (sym *Symbol) Description() string {
s := C.SymbolDescription(sym.Value.ptr)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}
// String returns Description().
func (sym *Symbol) String() string {
return sym.Description()
}
// value implements Valuer.
func (sym *Symbol) value() *Value {
return sym.Value
}