diff options
| author | Li Jin <dragon-fly@qq.com> | 2024-08-10 00:13:26 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2024-08-10 00:13:26 +0800 |
| commit | 8c9408bde7735065447891999042369ff64c95af (patch) | |
| tree | 4ce2a2c5f9abaa88c7e347e22002455f4bed2dd9 /src | |
| parent | e0d7551897dea8de214cc9d1fe24e51e74fc0e0b (diff) | |
| download | yuescript-8c9408bde7735065447891999042369ff64c95af.tar.gz yuescript-8c9408bde7735065447891999042369ff64c95af.tar.bz2 yuescript-8c9408bde7735065447891999042369ff64c95af.zip | |
fix macro function line number handling.
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 19 | ||||
| -rw-r--r-- | src/yuescript/yuescript.h | 7 |
2 files changed, 4 insertions, 22 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index a5b6666..9e778d7 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -5117,22 +5117,7 @@ private: | |||
| 5117 | newArgs.emplace_back(_parser.toString(argsDef->varArg)); | 5117 | newArgs.emplace_back(_parser.toString(argsDef->varArg)); |
| 5118 | } | 5118 | } |
| 5119 | } | 5119 | } |
| 5120 | std::string macroCodes; | 5120 | std::string macroCodes = "_ENV=yue:require('yue'),<index>:_G,<newindex>:(k,v)=>_G[k]=v\n("s + join(newArgs, ","sv) + ")->"s + _parser.toString(macroLit->body); |
| 5121 | { | ||
| 5122 | auto funLit = toAst<FunLit_t>("("s + join(newArgs, ","sv) + ")->"s, macroLit); | ||
| 5123 | auto block = macroLit->new_ptr<Block_t>(); | ||
| 5124 | if (auto stmt = macroLit->body->content.as<Statement_t>()) { | ||
| 5125 | block->statements.push_back(stmt); | ||
| 5126 | } else { | ||
| 5127 | auto blk = macroLit->body->content.to<Block_t>(); | ||
| 5128 | block->statements.dup(blk->statements); | ||
| 5129 | } | ||
| 5130 | block->statements.push_front(toAst<Statement_t>("_ENV=yue:require('yue'),<index>:_G,<newindex>:(k, v)=>_G[k]=v"sv, macroLit)); | ||
| 5131 | auto body = macroLit->new_ptr<Body_t>(); | ||
| 5132 | body->content.set(block); | ||
| 5133 | funLit->body.set(body); | ||
| 5134 | macroCodes = YueFormat{}.toString(funLit); | ||
| 5135 | } | ||
| 5136 | auto chunkName = "=(macro "s + macroName + ')'; | 5121 | auto chunkName = "=(macro "s + macroName + ')'; |
| 5137 | pushCurrentModule(); // cur | 5122 | pushCurrentModule(); // cur |
| 5138 | int top = lua_gettop(L) - 1; | 5123 | int top = lua_gettop(L) - 1; |
| @@ -5140,7 +5125,7 @@ private: | |||
| 5140 | pushYue("loadstring"sv); // cur loadstring | 5125 | pushYue("loadstring"sv); // cur loadstring |
| 5141 | lua_pushlstring(L, macroCodes.c_str(), macroCodes.size()); // cur loadstring codes | 5126 | lua_pushlstring(L, macroCodes.c_str(), macroCodes.size()); // cur loadstring codes |
| 5142 | lua_pushlstring(L, chunkName.c_str(), chunkName.size()); // cur loadstring codes chunk | 5127 | lua_pushlstring(L, chunkName.c_str(), chunkName.size()); // cur loadstring codes chunk |
| 5143 | pushOptions(macro->m_begin.m_line - 1); // cur loadstring codes chunk options | 5128 | pushOptions(macro->m_begin.m_line - 2); // cur loadstring codes chunk options |
| 5144 | if (lua_pcall(L, 3, 2, 0) != 0) { // loadstring(codes,chunk,options), cur f err | 5129 | if (lua_pcall(L, 3, 2, 0) != 0) { // loadstring(codes,chunk,options), cur f err |
| 5145 | std::string err = lua_tostring(L, -1); | 5130 | std::string err = lua_tostring(L, -1); |
| 5146 | throw CompileError("failed to load macro codes\n"s + err, macroLit); | 5131 | throw CompileError("failed to load macro codes\n"s + err, macroLit); |
diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index a160f55..97ebdef 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h | |||
| @@ -114,7 +114,7 @@ local function yue_call(f, ...) | |||
| 114 | end) | 114 | end) |
| 115 | end | 115 | end |
| 116 | yue_loadstring = function(...) | 116 | yue_loadstring = function(...) |
| 117 | local options, str, chunk_name, mode, env = get_options(...) | 117 | local options, str, chunk_name, env = get_options(...) |
| 118 | chunk_name = chunk_name or "=(yuescript.loadstring)" | 118 | chunk_name = chunk_name or "=(yuescript.loadstring)" |
| 119 | options.module = chunk_name | 119 | options.module = chunk_name |
| 120 | local code, err = yue.to_lua(str, options) | 120 | local code, err = yue.to_lua(str, options) |
| @@ -124,10 +124,7 @@ yue_loadstring = function(...) | |||
| 124 | if chunk_name then | 124 | if chunk_name then |
| 125 | yue.yue_compiled["@" .. chunk_name] = code | 125 | yue.yue_compiled["@" .. chunk_name] = code |
| 126 | end | 126 | end |
| 127 | return (loadstring or load)(code, chunk_name, unpack({ | 127 | return (loadstring or load)(code, chunk_name, "t", unpack({env})) |
| 128 | mode, | ||
| 129 | env | ||
| 130 | })) | ||
| 131 | end | 128 | end |
| 132 | local function yue_loadfile(fname, ...) | 129 | local function yue_loadfile(fname, ...) |
| 133 | local res, err = yue.read_file(fname) | 130 | local res, err = yue.read_file(fname) |
