Wrapped ph7_compile_file

This commit is contained in:
Brandon Dyck 2022-11-27 11:31:44 -07:00
parent 4b4b9abcbc
commit 869a2f3e67
3 changed files with 36 additions and 3 deletions

26
ph7.go
View File

@ -53,8 +53,6 @@ type Error struct {
Code ResultCode
}
// TODO create a CompileError that contains the error log text
func newError(code C.int) error {
if code == C.PH7_OK {
return nil
@ -100,7 +98,12 @@ func (e *Engine) ErrLog() (string, error) {
return C.GoStringN(s, n), nil
}
// TODO All the ph7_config verbs
/*
We don't bother ph7_config verbs other than PH7_CONFIG_ERR_LOG.
PH7_CONFIG_ERR_OUTPUT is another way of getting compile error messages,
which we don't need separately from returning CompileError values.
PH7_CONFIG_ERR_ABORT doesn't do anything.
*/
func (e *Engine) compileError(code C.int) error {
switch code {
@ -135,6 +138,23 @@ func (e *Engine) Compile(source []byte, phpOnly bool) (*VM, error) {
return newVM(cvm), nil
}
func (e *Engine) CompileFile(path string, phpOnly bool) (*VM, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var flags C.int
if phpOnly {
flags |= C.PH7_PHP_ONLY
}
var cvm *C.ph7_vm
err := e.compileError(C.ph7_compile_file((*C.ph7)(e), cpath, &cvm, flags))
if err != nil {
return nil, err
}
return newVM(cvm), nil
}
// TODO (*Engine).CompileFile
func (e *Engine) Close() error {

View File

@ -84,6 +84,18 @@ func TestCore(t *testing.T) {
}
}
func TestCompileFile(t *testing.T) {
engine, err := ph7.NewEngine()
mustSucceed(t, err)
defer mustSucceedF(t, engine.Close)
vm, err := engine.CompileFile("testdata/test_compile_file.php", false)
mustSucceed(t, err)
defer mustSucceedF(t, vm.Close)
expectOutput(t, vm, "Hello world!")
}
func TestCompileError(t *testing.T) {
engine, err := ph7.NewEngine()
mustSucceed(t, err)

1
testdata/test_compile_file.php vendored Normal file
View File

@ -0,0 +1 @@
<?php echo 'Hello world!'; ?>