@@ -6,7 +6,8 @@ import { Expect } from "alsatian";
66import { LuaTranspiler , TranspileError , LuaTarget } from "../../src/Transpiler" ;
77import { CompilerOptions } from "../../src/CommandLineParser" ;
88
9- const LuaVM = require ( "lua.vm.js" ) ;
9+ import { lauxlib , lua , lualib , to_luastring } from "fengari" ;
10+
1011const fs = require ( "fs" ) ;
1112
1213const libSource = fs . readFileSync ( path . join ( path . dirname ( require . resolve ( 'typescript' ) ) , 'lib.d.ts' ) ) . toString ( ) ;
@@ -50,16 +51,40 @@ export function transpileFile(path: string): string {
5051 diagnostics . forEach ( diagnostic => console . log ( `${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ) ) ;
5152
5253 const options : ts . CompilerOptions = { dontRequireLuaLib : true } ;
53- const lua = LuaTranspiler . transpileSourceFile ( program . getSourceFile ( path ) , checker , options ) ;
54- return lua . trim ( ) ;
54+ const result = LuaTranspiler . transpileSourceFile ( program . getSourceFile ( path ) , checker , options ) ;
55+ return result . trim ( ) ;
56+ }
57+
58+ export enum LuaReturnType {
59+ String ,
60+ Number ,
61+ Boolean
5562}
5663
57- export function executeLua ( lua : string , withLib = true ) : any {
64+ export function executeLua ( luaStr : string , type : LuaReturnType = LuaReturnType . String , withLib = true ) : any {
5865 if ( withLib ) {
59- lua = minimalTestLib + lua
66+ luaStr = minimalTestLib + luaStr ;
6067 }
61- const luavm = new LuaVM . Lua . State ( ) ;
62- return luavm . execute ( lua ) [ 0 ] ;
68+
69+ const L = lauxlib . luaL_newstate ( ) ;
70+ lualib . luaL_openlibs ( L ) ;
71+ lauxlib . luaL_dostring ( L , to_luastring ( luaStr ) ) ;
72+
73+ let result ;
74+
75+ switch ( type ) {
76+ case LuaReturnType . String :
77+ result = lua . lua_tojsstring ( L , - 1 ) ;
78+ break ;
79+ case LuaReturnType . Number :
80+ result = lua . lua_tonumber ( L , - 1 ) ;
81+ break ;
82+ case LuaReturnType . Boolean :
83+ result = lua . lua_toboolean ( L , - 1 ) ;
84+ break ;
85+ }
86+
87+ return result ;
6388}
6489
6590export function expectCodeEqual ( code1 : string , code2 : string ) {
@@ -74,8 +99,8 @@ export function expectCodeEqual(code1: string, code2: string) {
7499 Expect ( c1 ) . toBe ( c2 ) ;
75100}
76101
77- const lualib = fs . readFileSync ( "dist/lualib/typescript.lua" ) + "\n" ;
102+ const tslualib = fs . readFileSync ( "dist/lualib/typescript.lua" ) + "\n" ;
78103
79104const jsonlib = fs . readFileSync ( "test/src/json.lua" ) + "\n" ;
80105
81- export const minimalTestLib = lualib + jsonlib ;
106+ export const minimalTestLib = tslualib + jsonlib ;
0 commit comments