Skip to content

Commit ec1a528

Browse files
committed
fixed utf8 locals
fixes tomblind#47
1 parent ef604ee commit ec1a528

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

debugger/debugger.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ export namespace Debugger {
116116
Send.frames(frames);
117117
}
118118

119+
const supportsUtf8Identifiers = (() => {
120+
const identifier = `${string.char(226)}${string.char(143)}${string.char(176)}`;
121+
const [, err] = loadLuaString(`local ${identifier} = true return ${identifier}`);
122+
return err === undefined;
123+
})();
124+
125+
function isValidIdentifier(name: string) {
126+
if (supportsUtf8Identifiers) {
127+
for (const [c] of name.gmatch("[^a-zA-Z0-9_]")) {
128+
const [a] = c.byte();
129+
if (a && a < 128) {
130+
return false;
131+
}
132+
}
133+
return true;
134+
} else {
135+
const [invalidChar] = name.match("[^a-zA-Z0-9_]");
136+
return invalidChar === undefined;
137+
}
138+
}
139+
119140
function getLocals(level: number, thread?: Thread): Locals {
120141
const locs: Locals = {};
121142

@@ -149,8 +170,7 @@ export namespace Debugger {
149170
break;
150171
}
151172

152-
const [invalidChar] = name.match("[^a-zA-Z0-9_]");
153-
if (!invalidChar) {
173+
if (isValidIdentifier(name)) {
154174
locs[name] = {val, index, type: type(val)};
155175
}
156176

@@ -258,8 +278,7 @@ export namespace Debugger {
258278

259279
function mapName(sourceName: string, isProperty: boolean) {
260280
if (isProperty) {
261-
const [illegalChar] = sourceName.match("[^A-Za-z0-9_]");
262-
if (illegalChar) {
281+
if (isValidIdentifier(sourceName)) {
263282
return `["${sourceName}"]`;
264283
} else {
265284
return `.${sourceName}`;

tests/utf8/.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "LuaJit",
6+
"type": "lua-local",
7+
"request": "launch",
8+
"program": {
9+
"lua": "luajit",
10+
"file": "main.lua"
11+
},
12+
"verbose": true
13+
},
14+
]
15+
}

tests/utf8/main.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function ⏰(⏳, ⏫)
2+
local function ⏹()
3+
local= ""
4+
print(⏳, ⏫, ⏺)
5+
end
6+
⏹()
7+
end
8+
9+
⏰("", "")

0 commit comments

Comments
 (0)