runtime/lib_ffi.c - ktap

Global variables defined

Functions defined

Source code

  1. /*
  2. * lib_ffi.c - FFI library
  3. *
  4. * This file is part of ktap by Jovi Zhangwei.
  5. *
  6. * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
  7. *
  8. * ktap is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * ktap is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. */

  21. #include "../include/ktap_types.h"
  22. #include "../include/ktap_ffi.h"
  23. #include "ktap.h"
  24. #include "kp_vm.h"


  25. static int kplib_ffi_new(ktap_state_t *ks)
  26. {
  27.     int n = kp_arg_nr(ks);
  28.     csymbol_id cs_id = kp_arg_checknumber(ks, 1);
  29.     int array_size = kp_arg_checknumber(ks, 2);
  30.     int is_array = kp_arg_checknumber(ks, 3);
  31.     ktap_cdata_t *cd;

  32.     if (unlikely(n != 3)) {
  33.         /* this is not likely to happen since ffi.new arguments are
  34.          * generated by compiler */
  35.         set_nil(ks->top++);
  36.         kp_error(ks, "wrong number of arguments\n");
  37.         return 1;
  38.     }

  39.     if (unlikely(cs_id > max_csym_id(ks)))
  40.         kp_error(ks, "invalid csymbol id\n");

  41.     kp_verbose_printf(ks, "ffi.new symbol %s with length %d\n",
  42.             id_to_csym(ks, cs_id)->name, array_size);

  43.     if (is_array)
  44.         cd = kp_cdata_new_ptr(ks, NULL, array_size, cs_id, 1);
  45.     else
  46.         cd = kp_cdata_new_by_id(ks, NULL, cs_id);
  47.     set_cdata(ks->top, cd);
  48.     incr_top(ks);

  49.     return 1;
  50. }

  51. static int kplib_ffi_cast(ktap_state_t *ks)
  52. {
  53.     int n = kp_arg_nr(ks);
  54.     csymbol_id cs_id = kp_arg_checknumber(ks, 1);
  55.     unsigned long addr = kp_arg_checknumber(ks, 2);
  56.     ktap_cdata_t *cd;

  57.     if (unlikely(n != 2)) {
  58.         /* this is not likely to happen since ffi.cast arguments are
  59.          * generated by compiler */
  60.         set_nil(ks->top++);
  61.         kp_error(ks, "wrong number of arguments\n");
  62.         return 1;
  63.     }

  64.     if (unlikely(cs_id > max_csym_id(ks)))
  65.         kp_error(ks, "invalid csymbol id\n");

  66.     cd = kp_cdata_new_record(ks, (void *)addr, cs_id);
  67.     set_cdata(ks->top, cd);
  68.     incr_top(ks);
  69.     return 1;
  70. }

  71. static int kplib_ffi_sizeof(ktap_state_t *ks)
  72. {
  73.     /*@TODO finish this  08.11 2013 (houqp)*/
  74.     return 0;
  75. }

  76. static const ktap_libfunc_t ffi_lib_funcs[] = {
  77.     {"sizeof", kplib_ffi_sizeof},
  78.     {"new", kplib_ffi_new},
  79.     {"cast", kplib_ffi_cast},
  80.     {NULL}
  81. };

  82. int kp_lib_init_ffi(ktap_state_t *ks)
  83. {
  84.     return kp_vm_register_lib(ks, "ffi", ffi_lib_funcs);
  85. }