builtin_function.go 4.3 KB

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