style: Escape quotes and backslashes in usePeripheralWithSide method for safe Lua string interpolation
This commit is contained in:
@@ -807,13 +807,15 @@ export class Turtle extends EventEmitter {
|
|||||||
* Use a peripheral method by side
|
* Use a peripheral method by side
|
||||||
*/
|
*/
|
||||||
async usePeripheralWithSide(side, method, ...args) {
|
async usePeripheralWithSide(side, method, ...args) {
|
||||||
|
const safeSide = side.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
||||||
|
const safeMethod = method.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
||||||
const argsStr = args.map(a => {
|
const argsStr = args.map(a => {
|
||||||
if (a === null) return 'nil';
|
if (a === null) return 'nil';
|
||||||
if (typeof a === 'string') return `"${a}"`;
|
if (typeof a === 'string') return `"${a.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
||||||
return String(a);
|
return String(a);
|
||||||
}).join(', ');
|
}).join(', ');
|
||||||
|
|
||||||
return this.exec(`return peripheral.call("${side}", "${method}"${argsStr ? ', ' + argsStr : ''})`);
|
return this.exec(`return peripheral.call("${safeSide}", "${safeMethod}"${argsStr ? ', ' + argsStr : ''})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user