76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build script
|
|
// +build script
|
|
|
|
package js
|
|
|
|
import teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
|
|
|
func init() {
|
|
if !teaconst.IsMain {
|
|
return
|
|
}
|
|
|
|
SharedLibraryManager.Register(&JSConsoleAssertLibrary{})
|
|
}
|
|
|
|
type JSConsoleAssertLibrary struct {
|
|
JSBaseLibrary
|
|
}
|
|
|
|
func (this *JSConsoleAssertLibrary) JSNamespace() string {
|
|
return "gojs.console.JSConsoleAssertLibrary"
|
|
}
|
|
|
|
func (this *JSConsoleAssertLibrary) JSPrototype() string {
|
|
return `console.assert = function () {
|
|
$this.Assert(arguments)
|
|
}`
|
|
}
|
|
|
|
func (this *JSConsoleAssertLibrary) Assert(arguments *FunctionArguments) (any, error) {
|
|
arg0, ok := arguments.ArgAt(0)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
|
|
if arg0.IsArgumentsObject() {
|
|
var obj = arg0.Object()
|
|
if obj == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
assertion, err := obj.GetIdx(0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if assertion.IsBoolean() {
|
|
var b = assertion.Boolean()
|
|
if !b {
|
|
msgObj, err := obj.GetIdx(1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var msg = ""
|
|
|
|
// 支持 assert(assertion, msg)
|
|
// TODO 支持 assert(assertion, obj)
|
|
if msgObj.IsString() {
|
|
msg = msgObj.String()
|
|
}
|
|
|
|
msgValue, err := arguments.Context().NewValue("Assertion Failed: " + msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
arguments.Context().Isolate().ThrowException(msgValue)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, nil
|
|
}
|