本文整理汇总了Golang中github.com/gopherjs/gopherjs/compiler/typesutil.IsJsObject函数的典型用法代码示例。如果您正苦于以下问题:Golang IsJsObject函数的具体用法?Golang IsJsObject怎么用?Golang IsJsObject使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsJsObject函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: translateImplicitConversion
func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType types.Type) *expression {
if desiredType == nil {
return c.translateExpr(expr)
}
exprType := c.p.TypeOf(expr)
if types.Identical(exprType, desiredType) {
return c.translateExpr(expr)
}
basicExprType, isBasicExpr := exprType.Underlying().(*types.Basic)
if isBasicExpr && basicExprType.Kind() == types.UntypedNil {
return c.formatExpr("%e", c.zeroValue(desiredType))
}
switch desiredType.Underlying().(type) {
case *types.Slice:
return c.formatExpr("$subslice(new %1s(%2e.$array), %2e.$offset, %2e.$offset + %2e.$length)", c.typeName(desiredType), expr)
case *types.Interface:
if typesutil.IsJsObject(exprType) {
// wrap JS object into js.Object struct when converting to interface
return c.formatExpr("new $jsObjectPtr(%e)", expr)
}
if isWrapped(exprType) {
return c.formatExpr("new %s(%e)", c.typeName(exprType), expr)
}
if _, isStruct := exprType.Underlying().(*types.Struct); isStruct {
return c.formatExpr("new %1e.constructor.elem(%1e)", expr)
}
}
return c.translateExpr(expr)
}
示例2: translateSelection
func (c *funcContext) translateSelection(sel *types.Selection, pos token.Pos) ([]string, string) {
var fields []string
t := sel.Recv()
for _, index := range sel.Index() {
if ptr, isPtr := t.(*types.Pointer); isPtr {
t = ptr.Elem()
}
s := t.Underlying().(*types.Struct)
if jsTag := getJsTag(s.Tag(index)); jsTag != "" {
jsFieldName := s.Field(index).Name()
for {
fields = append(fields, fieldName(s, 0))
ft := s.Field(0).Type()
if typesutil.IsJsObject(ft) {
return fields, jsTag
}
ft = ft.Underlying()
if ptr, ok := ft.(*types.Pointer); ok {
ft = ptr.Elem().Underlying()
}
var ok bool
s, ok = ft.(*types.Struct)
if !ok || s.NumFields() == 0 {
c.p.errList = append(c.p.errList, types.Error{Fset: c.p.fileSet, Pos: pos, Msg: fmt.Sprintf("could not find field with type *js.Object for 'js' tag of field '%s'", jsFieldName), Soft: true})
return nil, ""
}
}
}
fields = append(fields, fieldName(s, index))
t = s.Field(index).Type()
}
return fields, ""
}
示例3: zeroValue
func (c *funcContext) zeroValue(ty types.Type) string {
if typesutil.IsJsObject(ty) {
return "null"
}
switch t := ty.Underlying().(type) {
case *types.Basic:
switch {
case is64Bit(t) || isComplex(t):
return fmt.Sprintf("new %s(0, 0)", c.typeName(ty))
case isBoolean(t):
return "false"
case isNumeric(t), t.Kind() == types.UnsafePointer:
return "0"
case isString(t):
return `""`
case t.Kind() == types.UntypedNil:
panic("Zero value for untyped nil.")
default:
panic("Unhandled type")
}
case *types.Array:
return fmt.Sprintf("%s.zero()", c.typeName(ty))
case *types.Signature:
return "$throwNilPointerError"
case *types.Slice:
return fmt.Sprintf("%s.nil", c.typeName(ty))
case *types.Struct:
return fmt.Sprintf("new %s.ptr()", c.typeName(ty))
case *types.Map:
return "false"
case *types.Interface:
return "$ifaceNil"
}
return fmt.Sprintf("%s.nil", c.typeName(ty))
}
示例4: externalize
func (c *funcContext) externalize(s string, t types.Type) string {
if typesutil.IsJsObject(t) {
return s
}
switch u := t.Underlying().(type) {
case *types.Basic:
if isNumeric(u) && !is64Bit(u) && !isComplex(u) {
return s
}
if u.Kind() == types.UntypedNil {
return "null"
}
}
return fmt.Sprintf("$externalize(%s, %s)", s, c.typeName(t))
}
示例5: translateAssignOfExpr
func (c *funcContext) translateAssignOfExpr(lhs, rhs ast.Expr, typ types.Type, define bool) string {
if l, ok := lhs.(*ast.IndexExpr); ok {
if t, ok := c.p.Types[l.X].Type.Underlying().(*types.Map); ok {
if typesutil.IsJsObject(c.p.Types[l.Index].Type) {
c.p.errList = append(c.p.errList, types.Error{Fset: c.p.fileSet, Pos: l.Index.Pos(), Msg: "cannot use js.Object as map key"})
}
keyVar := c.newVariable("_key")
return fmt.Sprintf(`%s = %s; (%s || $throwRuntimeError("assignment to entry in nil map"))[%s.keyFor(%s)] = { k: %s, v: %s };`, keyVar, c.translateImplicitConversionWithCloning(l.Index, t.Key()), c.translateExpr(l.X), c.typeName(t.Key()), keyVar, keyVar, c.translateImplicitConversionWithCloning(rhs, t.Elem()))
}
}
if _, ok := rhs.(*ast.CompositeLit); ok && define {
return fmt.Sprintf("%s = %s;", c.translateExpr(lhs), c.translateImplicitConversion(rhs, typ)) // skip $copy
}
return c.translateAssign(lhs, c.translateImplicitConversion(rhs, typ).String(), typ, define)
}
示例6: internalize
func (c *funcContext) internalize(s *expression, t types.Type) *expression {
if typesutil.IsJsObject(t) {
return s
}
switch u := t.Underlying().(type) {
case *types.Basic:
switch {
case isBoolean(u):
return c.formatExpr("!!(%s)", s)
case isInteger(u) && !is64Bit(u):
return c.fixNumber(c.formatExpr("$parseInt(%s)", s), u)
case isFloat(u):
return c.formatExpr("$parseFloat(%s)", s)
}
}
return c.formatExpr("$internalize(%s, %s)", s, c.typeName(t))
}
示例7: translateExpr
//.........这里部分代码省略.........
}
for _, element := range e.Elts {
kve := element.(*ast.KeyValueExpr)
for j := range elements {
if kve.Key.(*ast.Ident).Name == t.Field(j).Name() {
elements[j] = c.translateImplicitConversionWithCloning(kve.Value, t.Field(j).Type()).String()
break
}
}
}
}
return c.formatExpr("new %s.ptr(%s)", c.typeName(exprType), strings.Join(elements, ", "))
default:
panic(fmt.Sprintf("Unhandled CompositeLit type: %T\n", t))
}
case *ast.FuncLit:
_, fun := translateFunction(e.Type, nil, e.Body, c, exprType.(*types.Signature), c.p.FuncLitInfos[e], "")
if len(c.p.escapingVars) != 0 {
names := make([]string, 0, len(c.p.escapingVars))
for obj := range c.p.escapingVars {
names = append(names, c.p.objectNames[obj])
}
sort.Strings(names)
list := strings.Join(names, ", ")
return c.formatExpr("(function(%s) { return %s; })(%s)", list, fun, list)
}
return c.formatExpr("(%s)", fun)
case *ast.UnaryExpr:
t := c.p.TypeOf(e.X)
switch e.Op {
case token.AND:
if typesutil.IsJsObject(exprType) {
return c.formatExpr("%e.object", e.X)
}
switch t.Underlying().(type) {
case *types.Struct, *types.Array:
return c.translateExpr(e.X)
}
switch x := astutil.RemoveParens(e.X).(type) {
case *ast.CompositeLit:
return c.formatExpr("$newDataPointer(%e, %s)", x, c.typeName(c.p.TypeOf(e)))
case *ast.Ident:
obj := c.p.Uses[x].(*types.Var)
if c.p.escapingVars[obj] {
return c.formatExpr("(%1s.$ptr || (%1s.$ptr = new %2s(function() { return this.$target[0]; }, function($v) { this.$target[0] = $v; }, %1s)))", c.p.objectNames[obj], c.typeName(exprType))
}
return c.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, c.varPtrName(obj), c.typeName(exprType), c.objectName(obj), c.translateAssign(x, c.newIdent("$v", exprType), false))
case *ast.SelectorExpr:
sel, ok := c.p.Selections[x]
if !ok {
// qualified identifier
obj := c.p.Uses[x.Sel].(*types.Var)
return c.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, c.varPtrName(obj), c.typeName(exprType), c.objectName(obj), c.translateAssign(x, c.newIdent("$v", exprType), false))
}
newSel := &ast.SelectorExpr{X: c.newIdent("this.$target", c.p.TypeOf(x.X)), Sel: x.Sel}
c.setType(newSel, exprType)
c.p.Selections[newSel] = sel
return c.formatExpr("(%1e.$ptr_%2s || (%1e.$ptr_%2s = new %3s(function() { return %4e; }, function($v) { %5s }, %1e)))", x.X, x.Sel.Name, c.typeName(exprType), newSel, c.translateAssign(newSel, c.newIdent("$v", exprType), false))
case *ast.IndexExpr:
if _, ok := c.p.TypeOf(x.X).Underlying().(*types.Slice); ok {
return c.formatExpr("$indexPtr(%1e.$array, %1e.$offset + %2e, %3s)", x.X, x.Index, c.typeName(exprType))
}
示例8: Visit
func (c *FuncInfo) Visit(node ast.Node) ast.Visitor {
if node == nil {
if len(c.analyzeStack) != 0 {
c.analyzeStack = c.analyzeStack[:len(c.analyzeStack)-1]
}
return nil
}
c.analyzeStack = append(c.analyzeStack, node)
switch n := node.(type) {
case *ast.FuncDecl:
newInfo := c.p.newFuncInfo()
c.p.FuncDeclInfos[c.p.Defs[n.Name].(*types.Func)] = newInfo
return newInfo
case *ast.FuncLit:
newInfo := c.p.newFuncInfo()
c.p.FuncLitInfos[n] = newInfo
return newInfo
case *ast.BranchStmt:
switch n.Tok {
case token.GOTO:
for _, n2 := range c.analyzeStack {
c.Flattened[n2] = true
}
c.GotoLabel[c.p.Uses[n.Label].(*types.Label)] = true
case token.CONTINUE:
if n.Label != nil {
label := c.p.Uses[n.Label].(*types.Label)
for i := len(c.analyzeStack) - 1; i >= 0; i-- {
if labelStmt, ok := c.analyzeStack[i].(*ast.LabeledStmt); ok && c.p.Defs[labelStmt.Label] == label {
if _, ok := labelStmt.Stmt.(*ast.RangeStmt); ok {
return nil
}
stack := make([]ast.Node, len(c.analyzeStack))
copy(stack, c.analyzeStack)
c.ContinueStmts = append(c.ContinueStmts, continueStmt{labelStmt.Stmt.(*ast.ForStmt), stack})
return nil
}
}
return nil
}
for i := len(c.analyzeStack) - 1; i >= 0; i-- {
if _, ok := c.analyzeStack[i].(*ast.RangeStmt); ok {
return nil
}
if forStmt, ok := c.analyzeStack[i].(*ast.ForStmt); ok {
stack := make([]ast.Node, len(c.analyzeStack))
copy(stack, c.analyzeStack)
c.ContinueStmts = append(c.ContinueStmts, continueStmt{forStmt, stack})
return nil
}
}
}
case *ast.CallExpr:
callTo := func(obj types.Object) {
switch o := obj.(type) {
case *types.Func:
if recv := o.Type().(*types.Signature).Recv(); recv != nil {
if _, ok := recv.Type().Underlying().(*types.Interface); ok {
c.markBlocking(c.analyzeStack)
return
}
}
if o.Pkg() != c.p.Pkg {
if c.p.IsBlocking(o) {
c.markBlocking(c.analyzeStack)
}
return
}
stack := make([]ast.Node, len(c.analyzeStack))
copy(stack, c.analyzeStack)
c.LocalCalls[o] = append(c.LocalCalls[o], stack)
case *types.Var:
c.markBlocking(c.analyzeStack)
}
}
switch f := astutil.RemoveParens(n.Fun).(type) {
case *ast.Ident:
callTo(c.p.Uses[f])
case *ast.SelectorExpr:
if sel := c.p.Selections[f]; sel != nil && typesutil.IsJsObject(sel.Recv()) {
break
}
callTo(c.p.Uses[f.Sel])
case *ast.FuncLit:
ast.Walk(c, n.Fun)
for _, arg := range n.Args {
ast.Walk(c, arg)
}
if len(c.p.FuncLitInfos[f].Blocking) != 0 {
c.markBlocking(c.analyzeStack)
}
return nil
default:
if !astutil.IsTypeExpr(f, c.p.Info) {
c.markBlocking(c.analyzeStack)
}
}
case *ast.SendStmt:
c.markBlocking(c.analyzeStack)
//.........这里部分代码省略.........
示例9: translateAssign
func (c *funcContext) translateAssign(lhs, rhs ast.Expr, define bool) string {
lhs = astutil.RemoveParens(lhs)
if isBlank(lhs) {
panic("translateAssign with blank lhs")
}
if l, ok := lhs.(*ast.IndexExpr); ok {
if t, ok := c.p.TypeOf(l.X).Underlying().(*types.Map); ok {
if typesutil.IsJsObject(c.p.TypeOf(l.Index)) {
c.p.errList = append(c.p.errList, types.Error{Fset: c.p.fileSet, Pos: l.Index.Pos(), Msg: "cannot use js.Object as map key"})
}
keyVar := c.newVariable("_key")
return fmt.Sprintf(`%s = %s; (%s || $throwRuntimeError("assignment to entry in nil map"))[%s.keyFor(%s)] = { k: %s, v: %s };`, keyVar, c.translateImplicitConversionWithCloning(l.Index, t.Key()), c.translateExpr(l.X), c.typeName(t.Key()), keyVar, keyVar, c.translateImplicitConversionWithCloning(rhs, t.Elem()))
}
}
lhsType := c.p.TypeOf(lhs)
rhsExpr := c.translateImplicitConversion(rhs, lhsType)
if _, ok := rhs.(*ast.CompositeLit); ok && define {
return fmt.Sprintf("%s = %s;", c.translateExpr(lhs), rhsExpr) // skip $copy
}
isReflectValue := false
if named, ok := lhsType.(*types.Named); ok && named.Obj().Pkg() != nil && named.Obj().Pkg().Path() == "reflect" && named.Obj().Name() == "Value" {
isReflectValue = true
}
if !isReflectValue { // this is a performance hack, but it is safe since reflect.Value has no exported fields and the reflect package does not violate this assumption
switch lhsType.Underlying().(type) {
case *types.Array, *types.Struct:
if define {
return fmt.Sprintf("%s = $clone(%s, %s);", c.translateExpr(lhs), rhsExpr, c.typeName(lhsType))
}
return fmt.Sprintf("%s.copy(%s, %s);", c.typeName(lhsType), c.translateExpr(lhs), rhsExpr)
}
}
switch l := lhs.(type) {
case *ast.Ident:
return fmt.Sprintf("%s = %s;", c.objectName(c.p.ObjectOf(l)), rhsExpr)
case *ast.SelectorExpr:
sel, ok := c.p.SelectionOf(l)
if !ok {
// qualified identifier
return fmt.Sprintf("%s = %s;", c.objectName(c.p.Uses[l.Sel]), rhsExpr)
}
fields, jsTag := c.translateSelection(sel, l.Pos())
if jsTag != "" {
return fmt.Sprintf("%s.%s.%s = %s;", c.translateExpr(l.X), strings.Join(fields, "."), jsTag, c.externalize(rhsExpr.String(), sel.Type()))
}
return fmt.Sprintf("%s.%s = %s;", c.translateExpr(l.X), strings.Join(fields, "."), rhsExpr)
case *ast.StarExpr:
return fmt.Sprintf("%s.$set(%s);", c.translateExpr(l.X), rhsExpr)
case *ast.IndexExpr:
switch t := c.p.TypeOf(l.X).Underlying().(type) {
case *types.Array, *types.Pointer:
pattern := rangeCheck("%1e[%2f] = %3s", c.p.Types[l.Index].Value != nil, true)
if _, ok := t.(*types.Pointer); ok { // check pointer for nil (attribute getter causes a panic)
pattern = `%1e.nilCheck, ` + pattern
}
return c.formatExpr(pattern, l.X, l.Index, rhsExpr).String() + ";"
case *types.Slice:
return c.formatExpr(rangeCheck("%1e.$array[%1e.$offset + %2f] = %3s", c.p.Types[l.Index].Value != nil, false), l.X, l.Index, rhsExpr).String() + ";"
default:
panic(fmt.Sprintf("Unhandled lhs type: %T\n", t))
}
default:
panic(fmt.Sprintf("Unhandled lhs type: %T\n", l))
}
}