mirror of
https://github.com/laravel-ls/go-php
synced 2026-06-16 03:54:55 +02:00
35 lines
650 B
Go
35 lines
650 B
Go
package php
|
|
|
|
// #include "compile.h"
|
|
import "C"
|
|
|
|
import (
|
|
"errors"
|
|
"unsafe"
|
|
)
|
|
|
|
// The program struct contains the program in compiled format.
|
|
type program struct {
|
|
op_array *C.zend_op_array
|
|
}
|
|
|
|
// Compile a program from source code
|
|
func Compile(code []byte) (*program, error) {
|
|
cCode := C.CString(string(code))
|
|
defer C.free(unsafe.Pointer(cCode))
|
|
|
|
op_array := C.compile(cCode)
|
|
if op_array == nil {
|
|
return nil, errors.New("failed to compile code")
|
|
}
|
|
|
|
return &program{op_array: op_array}, nil
|
|
}
|
|
|
|
func (p *program) Free() {
|
|
if p.op_array != nil {
|
|
C.destroy_op_array(p.op_array)
|
|
C._efree(unsafe.Pointer(p.op_array))
|
|
}
|
|
p.op_array = nil
|
|
}
|