Skip to content

Instantly share code, notes, and snippets.

@alexnask
Created September 3, 2011 14:14
Show Gist options
  • Select an option

  • Save alexnask/1191243 to your computer and use it in GitHub Desktop.

Select an option

Save alexnask/1191243 to your computer and use it in GitHub Desktop.
Simple plugin system
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