defVar method

LuaObject defVar(
  1. String id,
  2. Object? value
)

Define a variable id with value. If the value is a LuaObject then its contents are copied. If a value is a lua function given by LuaObject.isFunc, then a FuncExpr definition must be present on that object. Otherwise an exception is thrown.

Implementation

LuaObject defVar(String id, Object? value) {
  final LuaObject luaObject;
  if (value is LuaObject) {
    if (value.skipSemanitcs) {
      luaObject = value.toRef();
    } else if (value.isFunc) {
      final closure = value.value as Function;
      final def = value.funcDef;
      final pscope = value.scope;
      if (def == null) {
        throw '''Internal error: programmer forgot to use LuaFuncBuilder. '''
            '''Please report this error!.''';
      }
      luaObject = LuaObject.func(id, def, closure, pscope);
    } else {
      luaObject = value;
    }
  } else /* not LuaObject */ {
    luaObject = LuaObject.variable(id, value);
  }
  vars[id] = luaObject;
  return luaObject;
}