Beispiel #1
0
static Value ripe_to_real(Value v)
{
    if (is_int64(v)) return v;
    if (is_double(v)) return v;
    Complex* c = val_to_complex(v);
    return double_to_val(c->real);
}
Beispiel #2
0
 void ConfUnit::set_int64(int64_t l) {
     if (is_int64()) {
         _union.l = l;
     }
     else {
         throw std::exception();
     }
 }
Beispiel #3
0
 int64_t ConfUnit::to_int64() const {
     if (is_int32()) {
         return (int64_t)_union.i;
     }
     else if (is_int64()) {
         return _union.l;
     }
     else {
         throw std::exception();
     }
 }
Beispiel #4
0
Datei: vm.c Projekt: merolle/ripe
int main(int argc, char** argv)
{
  // Provide argc and argv to interested modules
  sys_argc = argc;
  sys_argv = argv;

  // Initialize memory system
  mem_init();

  // Initialize stack and exception system
  stack_init();

  // Initialize static symbol table
  sym_init();

  // Initialize value klass system
  klass_init();

  // Phase 1
  stack_annot_push("init1_Function");
    init1_Function();
  stack_annot_pop();
  stack_annot_push("init1_Object");
    init1_Object();
  stack_annot_pop();

  ripe_module1a();
  ripe_module1b();

  // Phase 1.5
  stack_annot_push("phase 1.5");
    common_init_phase15();
    klass_init_phase15();
  stack_annot_pop();

  // Phase 2
  stack_annot_push("init2_Function");
    init2_Function();
  stack_annot_pop();
  stack_annot_push("init2_Object");
    init2_Object();
  stack_annot_pop();

  ripe_module2();
  ripe_module3();

  // Call main.
  Value rv = ripe_main();
  if (is_int64(rv)){
    return unpack_int64(rv);
  }
  mem_deinit();
  return 0;
}
Beispiel #5
0
 double ConfUnit::to_double() const {
     if (is_int32()) {
         return (double)_union.i;
     }
     else if (is_int64()) {
         return (double)_union.l;
     }
     else if (is_double()) {
         return _union.d;
     }
     else {
         throw std::exception();
     }
 }
Beispiel #6
0
void val_to_complex_soft(Value v, double* real, double* imag)
{
    if (is_double(v)) {
        *real = val_to_double(v);
        *imag = 0.0;
        return;
    }
    if (is_int64(v)) {
        *real = val_to_double_soft(v);
        *imag = 0.0;
        return;
    }
    Complex* c = val_to_complex(v);
    *real = c->real;
    *imag = c->imag;
}
Beispiel #7
0
// takes the first term off of the lua stack and return it as an
// erlang term in the state 'env'.
static int terminator_toerl_core(lua_State* lua, ERL_NIF_TERM *result, ErlNifEnv* env, ErlNifResourceType* resource_type)
{
	const int top = lua_gettop(lua);

	switch(lua_type(lua, top))
	{
		case LUA_TNIL:
		{
			*result = enif_make_atom(env, "nil");
			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TNUMBER:
		{
			const lua_Number number = luaL_checknumber(lua, top);

			if(is_int64(number))
			{
				*result = enif_make_int64(env, (int64_t)number);
			}
			else
			{
				*result = enif_make_double(env, (double)number);
			}

			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TBOOLEAN:
		{
			const int truefalse = lua_toboolean(lua, top);
			*result = truefalse ? enif_make_atom(env, "true") : enif_make_atom(env, "false");
			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TSTRING:
		{
			// get the lua string
			size_t string_len;
			const char * lua_string = lua_tolstring (lua, top, &string_len);

			// make space in erlang for it
			ErlNifBinary binary;
			if(enif_alloc_binary(string_len, &binary))
			{
				// clean it
				memset(binary.data, 0, binary.size);

				// copy it over
				memcpy(binary.data, lua_string, string_len);

				*result = enif_make_binary(env, &binary);
			}
			else
			{
				luaL_error (lua, "could not convert lua string");
			}
			lua_pop( lua, 1);

			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TTABLE:
		{
			size_t table_size = 0;

			int is_array = lua_is_array(lua);

			// table is at the top of the stack
			lua_pushnil(lua);  // nil as the first key
			while (lua_next(lua, top) != 0) {
				++table_size;
				lua_pop(lua, 1);
			}

			// make sure we can grow the stack
			luaL_checkstack(lua, 2, ERROR_STACK_MESSAGE);

			ERL_NIF_TERM *new_table = (ERL_NIF_TERM*) node_alloc(table_size * sizeof(ERL_NIF_TERM));
			ERL_NIF_TERM *next_cell = new_table;

			// table is at the top of the stack
			lua_pushnil(lua);  // nil as the first key
			while (lua_next(lua, top) != 0) 
			{
				// uses 'key' (at index -2) and 'value' (at index -1)
				if(is_array)
				{
					// remove 'value', keeps 'key' for next iteration
					ERL_NIF_TERM value;
					terminator_toerl_core(lua, &value, env, resource_type);
					*next_cell = value;
				}
				else
				{					
					// remove 'value', keeps 'key' for next iteration
					ERL_NIF_TERM tuple_value;
					terminator_toerl_core(lua, &tuple_value, env, resource_type);

					ERL_NIF_TERM tuple_key;
					lua_pushvalue( lua, -1);
					terminator_toerl_core(lua, &tuple_key, env, resource_type);

					*next_cell = enif_make_tuple2(env, tuple_key, tuple_value);
				}				

				next_cell++;
			}

			if(NULL != new_table)
			{
				*result = enif_make_list_from_array(env, new_table, table_size);
				node_free(new_table);
			}

			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TUSERDATA:
		{
			// add metatable to stack
			if(lua_getmetatable (lua, top))
			{

				// put the pid metatable onto the stack
				// compare the two metatables
				luaL_getmetatable(lua, TYPE_ERL_PID);
				if(lua_compare(lua, -1, -2, LUA_OPEQ))
				{
					const ErlNifPid* userdata = (const ErlNifPid*) lua_touserdata(lua, top);
					*result = enif_make_pid(env, userdata);
					lua_pop(lua, 3);
					assert(lua_gettop(lua) == top-1);
					return 1;
				}
				// pop the pid metatable
				lua_pop(lua, 1);			
				
				// push the ref metatable
				luaL_getmetatable(lua, TYPE_ERL_REF);
				if(lua_compare(lua, -1, -2, LUA_OPEQ))
				{
					erlref_ptr erlref = (erlref_ptr) lua_touserdata(lua, top);
					*result = enif_make_copy( env, erlref->reference );
					lua_pop(lua, 3);
					assert(lua_gettop(lua) == top-1);
					return 1;
				}
				lua_pop(lua, 1);

				// pop the ref metatable
				luaL_getmetatable(lua, TYPE_LUA_ADDRESS);
				if(lua_compare(lua, -1, -2, LUA_OPEQ))
				{
					mailbox_address_ptr address = (mailbox_address_ptr) lua_touserdata(lua, top);

					assert(NULL != address);
					state_work_ptr state_work = address->state_work;
					assert(NULL != state_work);
					void* resource;
					(*result) = state_make_resource( env, &resource, resource_type, state_work, WEAK_REF);
					assert(NULL != resource);
					lua_pop(lua, 3);

					assert(lua_gettop(lua) == top-1);
					return 1;
				}

				// pop the metatable
				lua_pop(lua, 1);
			}

			lua_pop( lua, 2);
			*result = enif_make_atom(env, "unknown_lua_userdata");
			
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TLIGHTUSERDATA:
		{
			*result = enif_make_atom(env, "lua_lightuserdata_notsupported");
			lua_pop(lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TTHREAD:
		{
			*result = enif_make_atom(env, "lua_thread_notsupported");
			lua_pop(lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TFUNCTION:
		{
			*result = enif_make_atom(env, "lua_function_notsupported");
			lua_pop(lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
	}
	assert(lua_gettop(lua) == top-1);
	return 0;
}