Jump to content

  • Log In with Google      Sign In   
  • Create Account

Wipe

Member Since 18 Aug 2011
Offline Last Active Private

Topics I've Started

cmake "game" target

25 October 2015 - 12:32 AM

Attached a tiny suggestion to change building samples/game/ optional [off by default] and rename it to "angelscript-game"; don't think AS should steal such a perfect name smile.png

[tests] PrintException() leftover

11 August 2014 - 11:35 PM

tests/test_feature/source/test_stdvector.cpp still tries to use PrintException() which has been replaced in r1971

Obviously whole thing does not compile now smile.png


Using global funcdef setter with imported function gives assert or invalid bytecode

11 June 2014 - 06:03 AM

I have this small piece of code
#include "utils.h"

const bool fail = true;

/*/ global funcdef var (setter) + imports /*/

const char* funcdef_import_assert_as =
"import void bar() from \"somewhere\";"
"void test1(){ @foo = test2; }"
"void test2(){ @foo = bar;   }"; // the problem

void set_funcdef_var(asIScriptFunction*) {}

bool funcdef_import_assert()
{
    COutStream cout;
    asIScriptEngine* engine = asCreateScriptEngine( ANGELSCRIPT_VERSION );
    engine->SetMessageCallback( asMETHOD(COutStream,Callback), &cout, asCALL_THISCALL );

    engine->RegisterFuncdef( "void MyVoid()" );
    engine->RegisterGlobalFunction( "void set_foo(MyVoid@)", asFUNCTION(set_funcdef_var), asCALL_CDECL );

    int r;
    asIScriptModule* module = engine->GetModule( "script", asGM_ALWAYS_CREATE );
    module->AddScriptSection( "script", funcdef_import_assert_as );

    if( module->Build() < 0 ) // assert (-DNDEBUG not present)
    {
	 PRINTF( "script1 Build fail\n" );
	 return( fail );
    }

    CBytecodeStream bytecode("");
    if( module->SaveByteCode( &bytecode ) < 0 )
    {
	 PRINTF( "bytecode save fail\n" );
   	 return( fail );
    }

    asIScriptModule* module_bytecode = engine->GetModule( "script_bytecode", asGM_ALWAYS_CREATE );
    if( module_bytecode->LoadByteCode( &bytecode ) < 0 ) // error (-DNDEBUG present)
    {
	PRINTF( "bytecode load fail\n" );
	 return( fail );
    }

    engine->Release();
    return( !fail );
}
Angelscript compiled with NDEBUG (my setup)
(0, 0) : Error   : LoadByteCode failed. The bytecode is invalid. Number of bytes read from stream: 187
Angelscript compiled without NDEBUG
testgnuc: ../../source/as_scriptfunction.cpp:468: virtual int asCScriptFunction::AddRef() const: Assertion `funcType != asFUNC_IMPORTED' failed.
-------------------------------------------------------


And related question:
Is such code the correct way of handling global funcdefs with configuration as above? I have a feeling that blindly calling Release() without AddRef() will lead me into trouble some sunny day (yet i need it or garbare collector will complain when releasing engine).
 
vector<asUINT> Script::Config::runFunctions;

/// called when changing script function used for loop(), draw(), etc.
void Script::Config::ChangeRunFunction( asUINT type, asIScriptFunction* func )
{
    asIScriptContext* ctx = asGetActiveContext();
    if( !ctx )
        return;

    asIScriptEngine* engine = ctx->GetEngine();
    if( !engine )
        return;

    asUINT oldFuncId = runFunctions[type];
    asIScriptFunction* oldFunc = engine->GetFunctionById( oldFuncId );
    if( oldFunc )
        oldFunc->Release();

    int funcId = 0;
    if( func )
        funcId = func->GetId();

    runFunctions[type] = funcId;
}

/// called before releasing engine
void Script::Config::Finish( asIScriptEngine* engine )
{
    for( auto funcId : runFunctions )
    {
        if( !funcId )
            continue;

        asIScriptFunction* func = engine->GetFunctionById( funcId );
        if( func )
            func->Release();
    }
}

Unregister function?

08 June 2014 - 06:27 AM

Is there a way to get rid of function added via asIScriptEngine::RegisterGlobalFunction()? I fail to find anything like that in docs or tests.

 


import + function with default argument

23 February 2013 - 03:19 AM

Default argument ignored(?) when imported function is using it. In case of int, no matter what value was given in function declaration, it's always 0.
// module 1
import void test2( bool dummy, int x = -1 ) from "test2";
	
void test( bool dummy, int x = -1 )
{
	Log( "x = "+x );
}

void check()
{
	Log( "TEST" );
	test( true );
	test( true, -2 );
	test( true, 2 );
	Log( "TEST2" );
	test2( false );
	test2( false, -2 );
	test2( false, 2 );
}

// module 2
void test2( bool dummy, int x = -1 )
{
	Log( "x = "+x );
}
Output is:
TEST
x = -1
x = -2
x = 2 
TEST2
x = 0
x = 0
x = 0 
Code above may refuse to compile for some reason. I got or crashes, or infinite loop (or maybe it just looks like this for me) on scripts loading (with both loading from source or bytecode), or no problems at all. Tested with r1558 and r1563.

PARTNERS