Project

General

Profile

Feature #2720 ยป testcase.txt

marchaland (David MARCHALAND), 02/08/2010 01:33 AM

 
1
### main.c ###
2

    
3
#include <stdio.h>
4
#include <dlfcn.h>
5

    
6

    
7
/* Call bridge function */
8
void call_bridge(const char* filename)
9
{
10
    // Open the library
11
    const char* libname = "bridge.so";
12
    printf("Opening library : %s\n", libname);
13
    void* handle;
14
    handle = dlopen(libname, RTLD_LAZY);
15
    handle ? printf("OK\n") : printf("KO\n\t%s\n", dlerror());
16
    if (handle)
17
    {
18
        // Load the symbol
19
        const char* symname = "ruby";
20
        printf("Load symbol : %s\n", symname);
21
        typedef void (*symbol_t)();
22
        symbol_t my_dlfunc = (symbol_t) dlsym(handle, symname);
23
        const char *dlsym_error = dlerror();
24
        !dlsym_error ? printf("OK\n") : printf("KO\n\t%s\n", dlsym_error);
25
        // Execute the function
26
        if (!dlsym_error) my_dlfunc(filename);
27
        // Close the library if opened
28
        printf("Closing library\n");
29
        int status = dlclose(handle);
30
        (status==0) ? printf("OK\n") : printf("KO\n\t%s\n", dlerror());
31
    }
32
}
33

    
34
/* Main function */
35
int main(int argc, char **argv)
36
{
37
    const int nb_run = 3;
38
    for (int i=0; i<nb_run; i++) call_bridge(argv[1]);
39
    return 0;
40
}
41

    
42

    
43
### bridge.c ###
44

    
45
#include "ruby.h"
46

    
47
/* Ruby interpreter routine */
48
void ruby(const char* filename)
49
{
50
    // INITIALIZATION
51
    RUBY_INIT_STACK
52
    ruby_init();
53
    ruby_init_loadpath();
54
    ruby_script(filename);
55
    // EXECUTION
56
    //VALUE content = rb_funcall(rb_cFile, rb_intern("read"), 1, rb_str_new2(filename));
57
    VALUE content = rb_str_new2("puts RUBY_VERSION; require 'fileutils'");
58
    //VALUE content = rb_str_new2("puts RUBY_VERSION");
59
    rb_define_const(rb_cObject, "RUBY_SCRIPT", content);
60
    printf("Before rb_eval_string()\n");
61
    rb_eval_string("eval(RUBY_SCRIPT, nil, $0, 1)");
62
    printf("After rb_eval_string()\n");
63
    // FINALIZATION ???
64
    //ruby_finalize();
65
    ruby_cleanup(0);
66
}
67

    
68
### Compilation script ###
69

    
70
#!/bin/csh -f
71

    
72
# Settings
73
set RUBY_H_FILE = <user defined>
74
set RUBY_SOLIB = <user defined>
75
setenv LD_LIBRARY_PATH ".:$RUBY_SOLIB:$LD_LIBRARY_PATH"
76
# Compilation
77
gcc -g -fPIC -Wall -std=c99 -I$RUBY_H_FILE -c bridge.c
78
ld -shared --export-dynamic -L$RUBY_SOLIB -lruby bridge.o -o bridge.so
79
gcc -g -Wall -std=c99 -ldl main.c -o main
80
# Simulation
81
./main