tomaka / hlua

Rust library to interface with Lua
MIT License
507 stars 48 forks source link

Add another example plus doc #104

Closed tomaka closed 7 years ago

tomaka commented 7 years ago

For the new example: https://github.com/tomaka/hlua/blob/ecb73849df75d3455e5cb41aa76839e7c452d880/hlua/examples/rust_function.rs

Generated code for the main function:

    movq    $-2, 560(%rbp)
    leaq    _ZN4hlua3Lua3new5alloc17ha827e9c21b1baa17E(%rip), %rcx
    xorl    %edx, %edx
    callq   lua_newstate
    movq    %rax, %rsi
    testq   %rsi, %rsi
    je  .LBB10_76
    leaq    _ZN4hlua3Lua3new5panic17h72b6f5044fd35a9fE(%rip), %rdx
    movq    %rsi, %rcx
    callq   lua_atpanic                         # lua context init finished
    movq    %rsi, 512(%rbp)
    movb    $1, 520(%rbp)
    movl    $-1001000, %edx
    movl    $2, %r8d
    movq    %rsi, %rcx
    callq   lua_rawgeti
    leaq    str.g(%rip), %rdx
    movl    $3, %r8d
    movq    %rsi, %rcx
    callq   lua_pushlstring
    xorl    %edx, %edx
    movq    %rsi, %rcx
    callq   lua_newuserdata                  # could maybe be removed, not sure
    leaq    _ZN4hlua15functions_write7wrapper17h8055a793efee9319E(%rip), %rdx
    movl    $1, %r8d
    movq    %rsi, %rcx
    callq   lua_pushcclosure
    movl    $-3, %edx
    movq    %rsi, %rcx
    callq   lua_settable                     # finishes writing to `foo`
    movl    $-2, %edx
    movq    %rsi, %rcx
    callq   lua_settop
    leaq    str.h(%rip), %rax
    movq    %rax, -80(%rbp)
    movl    $13, %eax
    movd    %rax, %xmm0
    movups  %xmm0, -72(%rbp)
    movq    $0, 72(%rbp)
    movq    $0, 32(%rsp)
    leaq    _ZN87_$LT$hlua..lua_functions..LuaCodeFromReader$LT$R$GT$$u20$as$u20$hlua..Push$LT$L$GT$$GT$11push_to_lua6reader17h444a64eaa86c77fbE(%rip), %rdx
    leaq    byte_str.a(%rip), %r9
    leaq    -80(%rbp), %r8
    movq    %rsi, %rcx
    callq   lua_load                   # loads the lua code in the context and parses it
    movl    %eax, %r14d
    movq    512(%rbp), %rbx
    cmpq    $1, 72(%rbp)
    je  .LBB10_78
    testl   %r14d, %r14d
    je  .LBB10_25

... jump ...

    movq    $0, 432(%rbp)
    leaq    512(%rbp), %rax
    movq    %rax, 440(%rbp)
    movl    $1, 448(%rbp)
    movq    %rbx, 456(%rbp)
    movl    448(%rbp), %eax
    movl    %eax, 540(%rbp)
    movl    $-1, %edx
    movq    %rbx, %rcx
    callq   lua_pushvalue                      # this could be removed
    movq    512(%rbp), %rcx
    movq    $0, 40(%rsp)
    movl    $0, 32(%rsp)
    xorl    %edx, %edx
    movl    $1, %r8d
    xorl    %r9d, %r9d
    callq   lua_pcallk                      # executes the lua code
    movq    512(%rbp), %rsi
    movl    %eax, 488(%rbp)
    testl   %eax, %eax
    je  .LBB10_45

... jump ...

    leaq    -80(%rbp), %r8
    movl    $-1, %edx
    movq    %rsi, %rcx
    callq   lua_tointegerx                   # turns the return type into an integer
    movq    %rax, %rdi
    cmpl    $0, -80(%rbp)
    je  .LBB10_57

... jump ...

    movl    $-2, %edx
    movq    %rsi, %rcx
    callq   lua_settop               # cleanup
    movl    $1, %r15d
    movl    $3, %r14d
    movl    540(%rbp), %edx
    testl   %edx, %edx
    je  .LBB10_61
    notl    %edx
    movq    %rbx, %rcx
    callq   lua_settop                      # cleanup again ; this could be merged with the one above, but unfortunately this can't be done I think
    testl   %r15d, %r15d
    jne .LBB10_72
    movl    %edi, 492(%rbp)
    leaq    492(%rbp), %rax
    movq    %rax, 400(%rbp)
    leaq    ref.i(%rip), %rax
    movq    %rax, 408(%rbp)
    cmpl    $40, %edi                 # comparison with 40 here
    jne .LBB10_83
    cmpb    $0, 520(%rbp)
    je  .LBB10_65
    movq    512(%rbp), %rcx
    callq   lua_close

Generated code for the wrapper+closure (symbol _ZN4hlua15functions_write7wrapper17h8055a793efee9319E that is loaded above):

    pushq   %rsi
    pushq   %rdi
    pushq   %rbx
    subq    $112, %rsp
    movq    %rcx, %rsi
    movl    $-1001001, %edx
    callq   lua_touserdata                      # could be removed? maybe?
    movq    %rsi, %rcx
    callq   lua_gettop                      # checks the number of arguments
    negl    %eax
    leaq    64(%rsp), %r8
    movq    %rsi, %rcx
    movl    %eax, %edx
    callq   lua_tointegerx                # loads the argument of the closure
    cmpl    $0, 64(%rsp)
    je  .LBB6_1
    shlq    $32, %rax
    leaq    (%rax,%rax,4), %rdx         # dark magic that multiplies by 5
    sarq    $32, %rdx
    movq    %rsi, %rcx
    callq   lua_pushinteger              # pushes the return value
    movl    $1, %eax
    addq    $112, %rsp
    popq    %rbx
    popq    %rdi
    popq    %rsi
    retq