package ph7 /* #include "ph7.h" int engine_config_err_log(ph7 *pEngine, const char **pzPtr, int *pLen) { return ph7_config(pEngine, PH7_CONFIG_ERR_LOG, pzPtr, pLen); } int vm_extract_output(ph7_vm *pVm, const char **pzPtr, int *pLen) { return ph7_vm_config(pVm, PH7_VM_CONFIG_EXTRACT_OUTPUT, pzPtr, pLen); } */ import "C" import ( "fmt" ) type Error struct { Value int } func (e Error) Error() string { return fmt.Sprintf("ph7 error: %d", e.Value) } func newError(value C.int) Error { return Error{ Value: int(value), } } type Engine struct { ptr *C.ph7 } func NewEngine() (*Engine, error) { engine := new(Engine) result := C.ph7_init(&engine.ptr) if result != C.PH7_OK { return nil, newError(result) } return engine, nil } func (e *Engine) ExtractErrLog() (string, error) { var s *C.char var n C.int result := C.engine_config_err_log(e.ptr, &s, &n) if result != C.PH7_OK { return "", newError(result) } return C.GoStringN(s, n), nil } func (e *Engine) Compile(source []byte, phpOnly bool) (*VM, error) { csource := C.CBytes(source) vm := new(VM) var flags C.int if phpOnly { flags |= C.PH7_PHP_ONLY } result := C.ph7_compile_v2(e.ptr, (*C.char)(csource), C.int(len(source)), &vm.ptr, flags) if result != C.PH7_OK { return nil, newError(result) } // TODO free csource return vm, nil } func (e *Engine) Close() error { result := C.ph7_release(e.ptr) if result != C.PH7_OK { return newError(result) } return nil } type VM struct { ptr *C.ph7_vm } func (vm *VM) ExtractOutput() (string, error) { var s *C.char var n C.int result := C.vm_extract_output(vm.ptr, &s, &n) if result != C.PH7_OK { return "", newError(result) } return C.GoStringN(s, n), nil } func (v *VM) Exec() error { result := C.ph7_vm_exec(v.ptr, (*C.int)(nil)) if result != C.PH7_OK { return newError(result) } return nil } func (v *VM) Close() error { result := C.ph7_vm_release(v.ptr) if result != C.PH7_OK { return newError(result) } return nil }