builtin_function.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package goja
  2. import (
  3. "fmt"
  4. )
  5. func (r *Runtime) builtin_Function(args []Value, proto *Object) *Object {
  6. src := "(function anonymous("
  7. if len(args) > 1 {
  8. for _, arg := range args[:len(args)-1] {
  9. src += arg.String() + ","
  10. }
  11. src = src[:len(src)-1]
  12. }
  13. body := ""
  14. if len(args) > 0 {
  15. body = args[len(args)-1].String()
  16. }
  17. src += "){" + body + "})"
  18. ret := r.toObject(r.eval(src, false, false, _undefined))
  19. ret.self.setProto(proto, true)
  20. return ret
  21. }
  22. func (r *Runtime) functionproto_toString(call FunctionCall) Value {
  23. obj := r.toObject(call.This)
  24. repeat:
  25. switch f := obj.self.(type) {
  26. case *funcObject:
  27. return newStringValue(f.src)
  28. case *nativeFuncObject:
  29. return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).toString()))
  30. case *boundFuncObject:
  31. return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).toString()))
  32. case *lazyObject:
  33. obj.self = f.create(obj)
  34. goto repeat
  35. case *proxyObject:
  36. var name string
  37. repeat2:
  38. switch c := f.target.self.(type) {
  39. case *funcObject:
  40. name = c.src
  41. case *nativeFuncObject:
  42. name = c.nameProp.get(call.This).String()
  43. case *boundFuncObject:
  44. name = c.nameProp.get(call.This).String()
  45. case *lazyObject:
  46. f.target.self = c.create(obj)
  47. goto repeat2
  48. default:
  49. name = f.target.String()
  50. }
  51. return newStringValue(fmt.Sprintf("function proxy() { [%s] }", name))
  52. }
  53. r.typeErrorResult(true, "Object is not a function")
  54. return nil
  55. }
  56. func (r *Runtime) createListFromArrayLike(a Value) []Value {
  57. o := r.toObject(a)
  58. if arr := r.checkStdArrayObj(o); arr != nil {
  59. return arr.values
  60. }
  61. l := toLength(o.self.getStr("length", nil))
  62. res := make([]Value, 0, l)
  63. for k := int64(0); k < l; k++ {
  64. res = append(res, o.self.getIdx(valueInt(k), nil))
  65. }
  66. return res
  67. }
  68. func (r *Runtime) functionproto_apply(call FunctionCall) Value {
  69. var args []Value
  70. if len(call.Arguments) >= 2 {
  71. args = r.createListFromArrayLike(call.Arguments[1])
  72. }
  73. f := r.toCallable(call.This)
  74. return f(FunctionCall{
  75. This: call.Argument(0),
  76. Arguments: args,
  77. })
  78. }
  79. func (r *Runtime) functionproto_call(call FunctionCall) Value {
  80. var args []Value
  81. if len(call.Arguments) > 0 {
  82. args = call.Arguments[1:]
  83. }
  84. f := r.toCallable(call.This)
  85. return f(FunctionCall{
  86. This: call.Argument(0),
  87. Arguments: args,
  88. })
  89. }
  90. func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
  91. var this Value
  92. var args []Value
  93. if len(boundArgs) > 0 {
  94. this = boundArgs[0]
  95. args = make([]Value, len(boundArgs)-1)
  96. copy(args, boundArgs[1:])
  97. } else {
  98. this = _undefined
  99. }
  100. return func(call FunctionCall) Value {
  101. a := append(args, call.Arguments...)
  102. return target(FunctionCall{
  103. This: this,
  104. Arguments: a,
  105. })
  106. }
  107. }
  108. func (r *Runtime) boundConstruct(target func([]Value, *Object) *Object, boundArgs []Value) func([]Value, *Object) *Object {
  109. if target == nil {
  110. return nil
  111. }
  112. var args []Value
  113. if len(boundArgs) > 1 {
  114. args = make([]Value, len(boundArgs)-1)
  115. copy(args, boundArgs[1:])
  116. }
  117. return func(fargs []Value, newTarget *Object) *Object {
  118. a := append(args, fargs...)
  119. copy(a, args)
  120. return target(a, newTarget)
  121. }
  122. }
  123. func (r *Runtime) functionproto_bind(call FunctionCall) Value {
  124. obj := r.toObject(call.This)
  125. fcall := r.toCallable(call.This)
  126. construct := obj.self.assertConstructor()
  127. l := int(toUInt32(obj.self.getStr("length", nil)))
  128. l -= len(call.Arguments) - 1
  129. if l < 0 {
  130. l = 0
  131. }
  132. v := &Object{runtime: r}
  133. ff := r.newNativeFuncObj(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(construct, call.Arguments), "", nil, l)
  134. v.self = &boundFuncObject{
  135. nativeFuncObject: *ff,
  136. wrapped: obj,
  137. }
  138. //ret := r.newNativeFunc(r.boundCallable(f, call.Arguments), nil, "", nil, l)
  139. //o := ret.self
  140. //o.putStr("caller", r.global.throwerProperty, false)
  141. //o.putStr("arguments", r.global.throwerProperty, false)
  142. return v
  143. }
  144. func (r *Runtime) initFunction() {
  145. o := r.global.FunctionPrototype.self
  146. o.(*nativeFuncObject).prototype = r.global.ObjectPrototype
  147. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  148. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  149. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  150. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  151. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  152. r.addToGlobal("Function", r.global.Function)
  153. }