関数関連のABC

関数に関係したABCを淡々と貼るよ。traitも調べないと無意味な予感はしてる。

サブルーチンっぽいやつ

function f(){
}
f();
  2         newfunction         var undefined():*       /* disp_id 0*/
  4         getglobalscope
  5         swap
  6         setslot             1
  8         findpropstrict      f
  10        callproperty        f (0)
...
var undefined():*       /* disp_id 0*/
{
  // local_count=1 max_scope=0 max_stack=0 code_len=1
  0         returnvoid
}

引数あり

function f(x){
    print(x);
}
f(10);
  8         findpropstrict      f
  10        pushbyte            10
  12        callproperty        f (1)
...
var undefined(*):*      /* disp_id 0*/
{
  // local_count=2 max_scope=0 max_stack=2 code_len=8
  0         findpropstrict      print
  2         getlocal1
  3         callproperty        print (1)
  6         pop
  7         returnvoid
}

値を返す

function f(){
   return 42
}
var undefined():*       /* disp_id 0*/
{
  // local_count=1 max_scope=0 max_stack=1 code_len=3
  0         pushbyte            42
  2         returnvalue
}

無名関数

匿名関数と書くと怒られるらしい。

var f = function(){}
f();
  2         newfunction         var undefined():*       /* disp_id 0*/
  4         getglobalscope
  5         swap
  6         setslot             1
  8         findpropstrict      f
  10        callproperty        f (0)

クロージャ

var x = 42;
var f = function(){
    return x;
}
f();
  2         pushbyte            42
  4         getglobalscope
  5         swap
  6         setslot             1
  8         newfunction         var undefined():*       /* disp_id 0*/
  10        getglobalscope
  11        swap
  12        setslot             2
  14        findpropstrict      f
  16        callproperty        f (0)
  19        coerce_a
  20        setlocal1
  21        getlocal1
  22        returnvalue
  23        kill                1
...

var undefined():*       /* disp_id 0*/
{
  // local_count=1 max_scope=0 max_stack=1 code_len=4
  0         getglobalscope
  1         getslot             1
  3         returnvalue
}