CapsAdmin

Untitled

Aug 19th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. local header = [[
  2. struct TCCState;
  3. typedef struct TCCState TCCState;
  4.  
  5. TCCState *tcc_new(void);
  6.  
  7. void tcc_delete(TCCState *s);
  8. void tcc_set_lib_path(TCCState *s, const char *path);
  9. void tcc_set_error_func(TCCState *s, void *error_opaque, void (*error_func)(void *opaque, const char *msg));
  10. int tcc_set_options(TCCState *s, const char *str);
  11. int tcc_add_include_path(TCCState *s, const char *pathname);
  12. int tcc_add_sysinclude_path(TCCState *s, const char *pathname);
  13. void tcc_define_symbol(TCCState *s, const char *sym, const char *value);
  14. void tcc_undefine_symbol(TCCState *s, const char *sym);
  15. int tcc_add_file(TCCState *s, const char *filename);
  16. int tcc_compile_string(TCCState *s, const char *buf);
  17. int tcc_set_output_type(TCCState *s, int output_type);
  18. int tcc_add_library_path(TCCState *s, const char *pathname);
  19. int tcc_add_library(TCCState *s, const char *libraryname);
  20. int tcc_add_symbol(TCCState *s, const char *name, const void *val);
  21. int tcc_output_file(TCCState *s, const char *filename);
  22. int tcc_run(TCCState *s, int argc, char **argv);
  23.  
  24.  
  25. int tcc_relocate(TCCState *s1, int *ptr);
  26.  
  27. void *tcc_get_symbol(TCCState *s, const char *name);
  28. ]]
  29.  
  30. ffi.cdef(header)
  31.  
  32. local module = ffi.load("libtcc")
  33.  
  34. local META = {}
  35. do -- meta
  36.     META.Type = "TCCState"
  37.     META.__index = META
  38.    
  39.     for def in header:gmatch("(tcc_.-)%(TCCState") do      
  40.         -- turn it into CamelCase
  41.         local friendly = def:gsub("(_.)", function(char)
  42.             return char:sub(2):upper()
  43.         end):sub(4)
  44.  
  45.         META[friendly] = function(self, ...)
  46.             local err = module[def](self.__ptr, ...)
  47.            
  48.             if type(err) == "number" and err == -1 then
  49.                 return err
  50.             end
  51.            
  52.             return err
  53.         end
  54.     end
  55.    
  56.     -- change output type to use strings as enums instead
  57.     local output_types = {"memory", "exe", "dll", "obj", "preprocess"}
  58.  
  59.     function META:SetOutputType(output_type)
  60.         return module.tcc_set_output_type(self.__ptr, output_types[output_type])
  61.     end
  62.    
  63.     if mmyy then
  64.        
  65.         -- add Remove with NULL behavior
  66.         function META:Remove()
  67.             module.tcc_delete(self.__ptr)
  68.             utilities.MakeNull(self)
  69.         end
  70.        
  71.         -- and remove Delete
  72.         META.Delete = nil
  73.     end
  74.    
  75.     function META:GetFunction(name, ret, args)
  76.         if not self.relocated then
  77.             self:Relocate(ffi.cast("int*", 1))         
  78.             self.relocated = true
  79.         end
  80.    
  81.         local ptr = self:GetSymbol(name)
  82.         local func = ffi.cast(("%s (*)(%s)"):format(ret, args or ""), ptr)
  83.        
  84.         return func
  85.     end
  86. end
  87.  
  88. function TCCState()
  89.     local self = setmetatable({}, META)
  90.     self.__ptr = module.tcc_new()
  91.    
  92.     if mmyy then
  93.         -- FIX /NUL HACK
  94.        
  95.         -- addons/*/include
  96.         self:AddIncludePath((R"include/NUL"):sub(0,-4))
  97.        
  98.         -- addons/*/lib
  99.         self:AddLibraryPath((R"lib/NUL"):sub(0,-4) .. ffi.arch .. "/")
  100.     end
  101.        
  102.     return self
  103. end
  104.  
  105. local state = TCCState()
  106.  
  107. state:CompileString([[
  108.     #include <stdio.h>
  109.    
  110.     float LOL(float* bar)
  111.     {  
  112.         return bar[0] + bar[1] + bar[2] + bar[3];
  113.     }
  114. ]])
  115.  
  116. local func = state:GetFunction("LOL", "float", "float[4]")
  117. print(func(ffi.new("float[4]", {2,2,4.5,4})))
Advertisement
Add Comment
Please, Sign In to add comment