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)
}
type Engine struct {
ptr *C.ph7
}
type Engine C.ph7
func NewEngine() (Engine, error) {
var engine Engine
result := C.ph7_init(&engine.ptr)
// TODO strip off the enclosing struct if possible after wrapping all engine methods.
func NewEngine() (*Engine, error) {
var cengine *C.ph7
result := C.ph7_init(&cengine)
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 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 {
return "", err
}
@ -94,24 +94,24 @@ func (e Engine) ErrLog() (string, error) {
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)
defer C.free(csource)
var flags C.int
if phpOnly {
flags |= C.PH7_PHP_ONLY
}
var ptr *C.ph7_vm
err := newError(C.ph7_compile_v2(e.ptr, (*C.char)(csource), C.int(len(source)), &ptr, flags))
var cvm *C.ph7_vm
err := newError(C.ph7_compile_v2((*C.ph7)(e), (*C.char)(csource), C.int(len(source)), &cvm, flags))
if err != nil {
return nil, err
}
return newVM(ptr), nil
return newVM(cvm), nil
}
func (e Engine) Close() error {
return newError(C.ph7_release(e.ptr))
func (e *Engine) Close() error {
return newError(C.ph7_release((*C.ph7)(e)))
}
type Value C.ph7_value