-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows e.g. setting the @@iterator property on an object, turning it into an Iterable.
- Loading branch information
Showing
10 changed files
with
413 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
v8 "rogchap.com/v8go" | ||
) | ||
|
||
func TestBuiltinSymbol(t *testing.T) { | ||
t.Parallel() | ||
|
||
iso := v8.NewIsolate() | ||
defer iso.Dispose() | ||
|
||
tsts := []struct { | ||
Func func(*v8.Isolate) *v8.Symbol | ||
WantDescription string | ||
}{ | ||
{v8.SymbolAsyncIterator, "Symbol.asyncIterator"}, | ||
{v8.SymbolHasInstance, "Symbol.hasInstance"}, | ||
{v8.SymbolIsConcatSpreadable, "Symbol.isConcatSpreadable"}, | ||
{v8.SymbolIterator, "Symbol.iterator"}, | ||
{v8.SymbolMatch, "Symbol.match"}, | ||
{v8.SymbolReplace, "Symbol.replace"}, | ||
{v8.SymbolSearch, "Symbol.search"}, | ||
{v8.SymbolSplit, "Symbol.split"}, | ||
{v8.SymbolToPrimitive, "Symbol.toPrimitive"}, | ||
{v8.SymbolToStringTag, "Symbol.toStringTag"}, | ||
{v8.SymbolUnscopables, "Symbol.unscopables"}, | ||
} | ||
for _, tst := range tsts { | ||
t.Run(tst.WantDescription, func(t *testing.T) { | ||
iter := tst.Func(iso) | ||
if iter.Description() != tst.WantDescription { | ||
t.Errorf("Description: got %q, want %q", iter.Description(), tst.WantDescription) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.