setupArgTable method

void setupArgTable({
  1. required List<String> originalArgs,
  2. String? scriptPath,
  3. List<String> scriptArgs = const [],
  4. List<String> codeStrings = const [],
})

Setup the global arg table following Lua's conventions

Implementation

void setupArgTable({
  required List<String> originalArgs,
  String? scriptPath,
  List<String> scriptArgs = const [],
  List<String> codeStrings = const [],
}) {
  final argTable = <dynamic, dynamic>{};
  final interpreter = _getInterpreterCommand();

  // For Lua compatibility we store the *display string* in arg[-1]/arg[0]
  // (joined with a single space) – when we actually spawn a subprocess
  // we use the list that `_getInterpreterCommand()` returned.
  final interpreterDisplay = interpreter.join(' ');

  if (scriptPath != null) {
    // Script file mode: arg[0] is script name, arg[1..n] are script arguments
    argTable[0] = scriptPath;
    for (var i = 0; i < scriptArgs.length; ++i) {
      argTable[i + 1] = scriptArgs[i];
    }
    argTable[-1] = interpreterDisplay;
  } else if (codeStrings.isNotEmpty) {
    // -e mode: arg[0] is interpreter name, arg[1] is -e, arg[2] is code
    argTable[0] = interpreterDisplay;
    var index = 1;
    for (final code in codeStrings) {
      argTable[index++] = '-e';
      argTable[index++] = code;
    }
  } else {
    // No script or code: arg[0] is interpreter name
    argTable[0] = interpreterDisplay;
  }

  print("Interpreter command: $interpreterDisplay");
  // Note: We don't set arg.n as it's nil in actual Lua 5.4.8

  // Set the global arg table
  bridge.setGlobal('arg', argTable);
}