push method

void push(
  1. dynamic value
)

Pushes a value to the stack, automatically converting Dart types to their equivalent Lua counterparts.

The following Dart types are supported: Null, bool, int, double, String, Function, JsFunction, LuaState, lua_State, LuaOpaqueValue.

Dart types that are not supported are converted to light userdata which can be retrieved again from Dart later.

Implementation

void push(dynamic value) {
  if (value == null) {
    lua_pushnil(L);
  } if (value is bool) {
    lua_pushboolean(L, value ? 1 : 0);
  } else if (value is int) {
    lua_pushinteger(L, value);
  } else if (value is double) {
    lua_pushnumber(L, value);
  } else if (value is JsFunction) {
    lua_pushcfunction(L, value);
  } else if (value is Function) {
    lua_pushcfunction(L, _convertCFunction(value));
  } else if (value is String) {
    lua_pushstring(L, to_luastring(value));
  } else if (value is LuaState) {
    if (value == this) {
      pushThread();
    } else {
      // You can only push the current thread on the stack so we have to do
      // that to the target state and then xmove it to the current state.
      value.pushThread();
      value.xmove(this, 1);
    }
  } else if (value is lua_State) {
    if (value == L) {
      pushThread();
    } else {
      // Same as before but without the wrapper
      lua_pushthread(value);
      lua_xmove(value, L, 1);
    }
  } else if (value is LuaOpaqueValue) {
    var out = at(value.index);
    // Verify if the stack still contains the value we want to push
    if (value == out) {
      dup(value.index);
    } else {
      throw LuaException("Attempted to push invalid LuaOpaqueValue");
    }
  } else {
    lua_pushlightuserdata(L, value);
  }
}