github.com/cloudwego/frugal@v0.1.15/internal/binary/decoder/linker_emu.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package decoder
    18  
    19  import (
    20      `unsafe`
    21  
    22      `github.com/cloudwego/frugal/internal/atm/emu`
    23      `github.com/cloudwego/frugal/internal/atm/hir`
    24      `github.com/cloudwego/frugal/internal/rt`
    25  )
    26  
    27  func link_emu(prog hir.Program) Decoder {
    28      return func(buf unsafe.Pointer, nb int, i int, p unsafe.Pointer, rs *RuntimeState, st int) (pos int, err error) {
    29          ctx := emu.LoadProgram(prog)
    30          ret := (*rt.GoIface)(unsafe.Pointer(&err))
    31          ctx.Ap(0, buf)
    32          ctx.Au(1, uint64(nb))
    33          ctx.Au(2, uint64(i))
    34          ctx.Ap(3, p)
    35          ctx.Ap(4, unsafe.Pointer(rs))
    36          ctx.Au(5, uint64(st))
    37          ctx.Run()
    38          pos = int(ctx.Ru(0))
    39          ret.Itab = (*rt.GoItab)(ctx.Rp(1))
    40          ret.Value = ctx.Rp(2)
    41          ctx.Free()
    42          return
    43      }
    44  }
    45  
    46  func emu_decode(ctx hir.CallContext) (int, error) {
    47      return decode(
    48          (*rt.GoType)(ctx.Ap(0)),
    49          ctx.Ap(1),
    50          int(ctx.Au(2)),
    51          int(ctx.Au(3)),
    52          ctx.Ap(4),
    53          (*RuntimeState)(ctx.Ap(5)),
    54          int(ctx.Au(6)),
    55      )
    56  }
    57  
    58  func emu_mkreturn(ctx hir.CallContext) func(int, error) {
    59      return func(ret int, err error) {
    60          ctx.Ru(0, uint64(ret))
    61          emu_seterr(ctx, 1, err)
    62      }
    63  }
    64  
    65  func emu_gcall_decode(ctx hir.CallContext) {
    66      if !ctx.Verify("**ii**i", "i**") {
    67          panic("invalid decode call")
    68      } else {
    69          emu_mkreturn(ctx)(emu_decode(ctx))
    70      }
    71  }