Stripped containing struct from Engine

This commit is contained in:
Brandon Dyck 2022-11-27 08:30:53 -07:00
parent 30bb620dba
commit ca847dc7c5

32
ph7.go
View File

@ -68,25 +68,25 @@ func (e Error) Error() string {
return fmt.Sprintf("ph7: %s", e.Code) return fmt.Sprintf("ph7: %s", e.Code)
} }
type Engine struct { type Engine C.ph7
ptr *C.ph7
}
func NewEngine() (Engine, error) { // TODO strip off the enclosing struct if possible after wrapping all engine methods.
var engine Engine
result := C.ph7_init(&engine.ptr) func NewEngine() (*Engine, error) {
var cengine *C.ph7
result := C.ph7_init(&cengine)
if result != C.PH7_OK { if result != C.PH7_OK {
return Engine{}, newError(result) return nil, newError(result)
} }
return engine, nil return (*Engine)(cengine), nil
} }
func (e Engine) ErrLog() (string, error) { func (e *Engine) ErrLog() (string, error) {
var s *C.char var s *C.char
var n C.int var n C.int
err := newError(C.engine_config_err_log(e.ptr, &s, &n)) err := newError(C.engine_config_err_log((*C.ph7)(e), &s, &n))
if err != nil { if err != nil {
return "", err return "", err
} }
@ -94,24 +94,24 @@ func (e Engine) ErrLog() (string, error) {
return C.GoStringN(s, n), nil return C.GoStringN(s, n), nil
} }
func (e Engine) Compile(source []byte, phpOnly bool) (*VM, error) { func (e *Engine) Compile(source []byte, phpOnly bool) (*VM, error) {
csource := C.CBytes(source) csource := C.CBytes(source)
defer C.free(csource) defer C.free(csource)
var flags C.int var flags C.int
if phpOnly { if phpOnly {
flags |= C.PH7_PHP_ONLY flags |= C.PH7_PHP_ONLY
} }
var ptr *C.ph7_vm var cvm *C.ph7_vm
err := newError(C.ph7_compile_v2(e.ptr, (*C.char)(csource), C.int(len(source)), &ptr, flags)) err := newError(C.ph7_compile_v2((*C.ph7)(e), (*C.char)(csource), C.int(len(source)), &cvm, flags))
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newVM(ptr), nil return newVM(cvm), nil
} }
func (e Engine) Close() error { func (e *Engine) Close() error {
return newError(C.ph7_release(e.ptr)) return newError(C.ph7_release((*C.ph7)(e)))
} }
type Value C.ph7_value type Value C.ph7_value