Created
September 3, 2011 14:14
-
-
Save alexnask/1191243 to your computer and use it in GitHub Desktop.
Simple plugin system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use tcc | |
| import tcc | |
| import structs/HashMap | |
| Plugin: class { | |
| state: Tcc | |
| name: String | |
| mem: Pointer | |
| size: Int | |
| methods := HashMap<String,Func(This)> new() | |
| init: func(=name) { | |
| state = Tcc new() | |
| state setOutputType(OutputType memory) | |
| } | |
| reserve: func(s: String) { | |
| methods[s] = null | |
| } | |
| call: func(s: String) { | |
| if(methods[s] as Closure thunk) methods[s](this) | |
| } | |
| loadFromText: func(s: String) { | |
| state compileString(s) | |
| size = state relocate(null) | |
| mem = gc_malloc(size) | |
| state relocate(mem) | |
| fill() | |
| } | |
| loadFromLib: func(lib: String) { | |
| state addLibrary(lib) | |
| size = state relocate(null) | |
| mem = gc_malloc(size) | |
| state relocate(mem) | |
| fill() | |
| } | |
| fill: func { | |
| methods each(|method,f| | |
| f as Closure thunk = state getSymbol(name+"_"+method) | |
| ) | |
| } | |
| } | |
| plugin := Plugin new("test") | |
| plugin reserve("do") | |
| plugin loadFromText("void test_do(void* this) { printf(\"Hello World! \\n\"); }") | |
| // Smething is not loaded correctly :-/ | |
| plugin call("do") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment