engine/source/scriptlib/ImportCache.c
/**
 * Loads and compiles a script file and indexes its functions so that they can
 * be imported.
 */
Loads and compiles a script file and indexes its functions so that they can
be imported.
Loads and compiles a script file and indexes its functions so that they can be imported .
HRESULT ImportNode_Init(ImportNode *self, const char *path)
{
    char *scriptText;
    int i, size;
    List *list; 
    List_Init(&self->functions);
    Interpreter_Init(&self->interpreter, path, builtins);
    self->interpreter.theParser.isImport = TRUE;
    scriptText = readscript(path);
    if(scriptText == NULL)
    {
        goto error;
    }
    if(FAILED(Interpreter_ParseText(&self->interpreter, scriptText, 1, path)))
    {
        printf("Script error: failed to import '%s': parsing failed\n", path);
        goto error;
    }
    free(scriptText);
    scriptText = NULL;
    list = &self->interpreter.theInstructionList;
    size = List_GetSize(list);
    List_Reset(list);
    for(i = 0; i < size; i++)
    {
        Instruction *inst = (Instruction *)List_Retrieve(list);
        if(inst->OpCode == FUNCDECL)
        {
#ifdef IC_DEBUG
            fprintf(stderr, "ImportNode_Init: %s: %s@%i\n", path, List_GetName(list), i);
#endif
            List_InsertAfter(&self->functions, (void *)(size_t)i, List_GetName(list));
        }
        List_GotoNext(list);
    }
    if(FAILED(Interpreter_CompileInstructions(&self->interpreter)))
    {
        printf("Script error: failed to import '%s': failed to compile\n", path);
        goto error;
    }
    assert(list->solidlist != NULL);
    List_Reset(&self->functions);
    size = List_GetSize(&self->functions);
    for(i = 0; i < size; i++)
    {
        int index = (size_t)List_Retrieve(&self->functions);
        List_Update(&self->functions, &(list->solidlist[index]));
        assert(((Instruction *)(list->solidlist[index]))->OpCode == FUNCDECL);
        List_GotoNext(&self->functions);
    }
    return S_OK;
error:
    if(scriptText)
    {
        free(scriptText);
    }
    Interpreter_Clear(&self->interpreter);
    List_Clear(&self->functions);
    return E_FAIL;
}
HRESULT ImportNode_Init ( ImportNode * self , const char * path ) { char * scriptText ; int i , size ; List * list ; List_Init ( & self -> functions ) ; Interpreter_Init ( & self -> interpreter , path , builtins ) ; self -> interpreter . theParser . isImport = TRUE ; scriptText = readscript ( path ) ; if ( scriptText == NULL ) { goto error ; } if ( FAILED ( Interpreter_ParseText ( & self -> interpreter , scriptText , 1 , path ) ) ) { printf ( " \n " , path ) ; goto error ; } free ( scriptText ) ; scriptText = NULL ; list = & self -> interpreter . theInstructionList ; size = List_GetSize ( list ) ; List_Reset ( list ) ; for ( i = 0 ; i < size ; i ++ ) { Instruction * inst = ( Instruction * ) List_Retrieve ( list ) ; if ( inst -> OpCode == FUNCDECL ) { #ifdef IC_DEBUG fprintf ( stderr , " \n " , path , List_GetName ( list ) , i ) ; #endif List_InsertAfter ( & self -> functions , ( void * ) ( size_t ) i , List_GetName ( list ) ) ; } List_GotoNext ( list ) ; } if ( FAILED ( Interpreter_CompileInstructions ( & self -> interpreter ) ) ) { printf ( " \n " , path ) ; goto error ; } assert ( list -> solidlist != NULL ) ; List_Reset ( & self -> functions ) ; size = List_GetSize ( & self -> functions ) ; for ( i = 0 ; i < size ; i ++ ) { int index = ( size_t ) List_Retrieve ( & self -> functions ) ; List_Update ( & self -> functions , & ( list -> solidlist [ index ] ) ) ; assert ( ( ( Instruction * ) ( list -> solidlist [ index ] ) ) -> OpCode == FUNCDECL ) ; List_GotoNext ( & self -> functions ) ; } return S_OK ; error : if ( scriptText ) { free ( scriptText ) ; } Interpreter_Clear ( & self -> interpreter ) ; List_Clear ( & self -> functions ) ; return E_FAIL ; }
Loads and compiles a script file and indexes its functions so that they can
 be imported.
Loads and compiles a script file and indexes its functions so that they can be imported .
// more readable than "&self->interpreter.theInstructionList" // get indices of the function declarations in the instruction list // finish compiling and convert indices to pointers to the function entry points