Skip to content

Commit

Permalink
C++ encode
Browse files Browse the repository at this point in the history
  • Loading branch information
SnorlaxAssist committed Jul 20, 2024
1 parent d49671d commit 67cbbe4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 174 deletions.
2 changes: 1 addition & 1 deletion tests/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ FIU_TESTCASES {
"Specs/nativeNamecall",
"Specs/vectorLib",
"Specs/importConstants",
"Specs/encodeOp",
"Specs/decodeOp",
}},
{"Benchmarks", {
// Fiu Benchmark Tests
Expand Down
44 changes: 44 additions & 0 deletions tests/Specs/decodeOp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--!ctx Luau

local ok, compileResult = Luau.compile([[
local a = {0}
a[1] = 1
spark(a)
]], {
optimizationLevel = 2,
debugLevel = 2,
coverageLevel = 0,
vectorLib = nil,
vectorCtor = nil,
vectorType = nil
}, 2)

if not ok then
error(compileResult)
end

local settings = Fiu.luau_newsettings()

local sparkCalled = false
settings.extensions["spark"] = function(t)
if t[1] == 1 then
sparkCalled = true
return
end
error(`failed`)
end

local decodeCalled = false
settings.decodeOp = function(op)
decodeCalled = true
return op - 2
end

local func, _ = Fiu.luau_load(Fiu.luau_deserialize(compileResult, settings), {}, settings)

func()

assert(decodeCalled, "extension `decodeOp` was not called")
assert(sparkCalled, "extension `spark` was not called")

OK()
169 changes: 0 additions & 169 deletions tests/Specs/encodeOp.lua

This file was deleted.

32 changes: 28 additions & 4 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "luau/CLI/FileUtils.h"
#include "luau/CLI/Coverage.h"
#include "Luau/Compiler.h"
#include "Luau/BytecodeBuilder.h"
#include "Luau/CodeGen.h"
#include "Luau/ExperimentalFlags.h"

Expand Down Expand Up @@ -81,15 +82,15 @@ const char* const fiuUnsupportedExperimentalTests[] = {
"Conformance/Deserializer",
};

TestCompileResult compileTest(string source, Luau::CompileOptions opts, const char* name = "Compiled", bool codegen = false)
TestCompileResult compileTest(string source, Luau::CompileOptions opts, const char* name = "Compiled", Luau::BytecodeEncoder *encoder = nullptr, bool codegen = false)
{
lua_State* L = luaL_newstate();
if (codegen)
if (Luau::CodeGen::isSupported())
Luau::CodeGen::create(L);
else
return {false, "Platform does not support CodeGen"};
string bytecode = Luau::compile(source, opts);
string bytecode = Luau::compile(source, opts, {}, encoder);
if (luau_load(L, name, bytecode.data(), bytecode.size(), 0) == 0)
{
if (codegen)
Expand Down Expand Up @@ -291,10 +292,28 @@ int testOk(lua_State* L)
return 0;
}

struct OffsetBytecodeEncoder : Luau::BytecodeEncoder
{
public:
OffsetBytecodeEncoder() : offset(0) {}
void setOffset(uint32_t s) {
offset = s;
}
void encode(uint32_t* data, size_t count) override
{
for (size_t i = 0; i < count; ++i)
data[i] += offset;
return;
}
private:
uint32_t offset;
};

int luauCompile(lua_State* L)
{
const char* source = luaL_checkstring(L, 1);
Luau::CompileOptions options = compleOptions();
OffsetBytecodeEncoder encoder;
if (!lua_isnone(L, 2) && !lua_isnil(L, 2))
{
options = {};
Expand Down Expand Up @@ -322,7 +341,12 @@ int luauCompile(lua_State* L)
options.vectorType = luaL_checkstring(L, -1);
lua_pop(L, 6);
}
TestCompileResult compileResult = compileTest(source, options);
if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
{
int num = luaL_checknumber(L, 3);
encoder.setOffset(num);
}
TestCompileResult compileResult = compileTest(source, options, "Compiled", &encoder);
if (compileResult.success)
{
lua_pushboolean(L, 1);
Expand Down Expand Up @@ -973,7 +997,7 @@ int main(int argc, char* argv[])
}

// Compile Fiu (CodeGen)
TestCompileResult fiuCodeGenCompileResult = compileTest(fiuSource, compleOptions(), "FiuCodeGen", true);
TestCompileResult fiuCodeGenCompileResult = compileTest(fiuSource, compleOptions(), "FiuCodeGen", nullptr, true);
if (!fiuCodeGenCompileResult.success)
{
printf("[%s] Failed to Compile 'Fiu'(CodeGen): %s\n", WARN_SYMBOL, fiuCodeGenCompileResult.data.c_str());
Expand Down

0 comments on commit 67cbbe4

Please sign in to comment.