All pastes #572883 Raw Edit

Something

public text v1 · immutable
·published 2007-06-17 22:06 UTC
rendered paste body
1st example:

// s1.lua
print( "Script 1" );

// s2.lua
print( "Script 2" );

// multi.c
// Compile:
// gcc -Wall -g -o multi multi.c `lua-config --include --libs`

#include <assert.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>

int main( int argc, char** argv ) {

  lua_State* state1;
  lua_State* state2;
  int err;
  int i;

  // Prepare script 1

  state1 = lua_open();
  assert( state1 );

  luaopen_base( state1 );

  err = luaL_loadfile( state1, "s1.lua" );
  assert( err == 0 );

  // Prepare script 2

  state2 = lua_open();
  assert( state2 );

  luaopen_base( state2 );

  err = luaL_loadfile( state2, "s2.lua" );
  assert( err == 0 );

  for( i = 0; i < 10; i++ ) {

    // Run script 1

    lua_pushvalue( state1, -1 );
    lua_call( state1, 0, 0 );

    // Run script 2

    lua_pushvalue( state2, -1 );
    lua_call( state2, 0, 0 );
  } 

  lua_close( state1 );
  lua_close( state2 ); 

  return 0;
}


2nd example, uses functions instead:

// s1.lua
function printName() 
  print( "Script 1" );
  return;
end;

// s2.lua
function printName() 
  print( "Script 2" );
  return;
end;

// multi.c
// Compile:
// gcc -Wall -g -o multi multi.c `lua-config --include --libs`

#include <assert.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>

int main( int argc, char** argv ) {

  lua_State* state1;
  lua_State* state2;
  int err;
  int i;

  // Prepare script 1

  state1 = lua_open();
  assert( state1 );

  luaopen_base( state1 );

  err = luaL_loadfile( state1, "s1.lua" );
  assert( err == 0 );

  lua_call( state1, 0, 0 );

  // Prepare script 2

  state2 = lua_open();
  assert( state2 );

  luaopen_base( state2 );

  err = luaL_loadfile( state2, "s2.lua" );
  assert( err == 0 );

  lua_call( state2, 0, 0 );

  for( i = 0; i < 10; i++ ) {

    int top;

    // Run script 1

    top = lua_gettop( state1 );

    lua_getglobal( state1, "printName" );
    lua_call( state1, 0, 0 );

    lua_settop( state1, top );

    // Run script 2

    top = lua_gettop( state2);

    lua_getglobal( state2, "printName" );
    lua_call( state2, 0, 0 );

    lua_settop( state2, top );
  } 

  lua_close( state1 );
  lua_close( state2 ); 

  return 0;
}