2015-09-17 21:22:07 +00:00
|
|
|
package php
|
|
|
|
|
|
|
|
// #include <stdio.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include "engine.h"
|
|
|
|
// #include "context.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
context *C.struct__engine_context
|
|
|
|
writer io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Exec(filename string) error {
|
|
|
|
name := C.CString(filename)
|
|
|
|
defer C.free(unsafe.Pointer(name))
|
|
|
|
|
|
|
|
_, err := C.context_exec(c.context, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error executing script '%s' in context", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Destroy() {
|
|
|
|
C.context_destroy(c.context)
|
|
|
|
c = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//export contextWrite
|
2015-09-19 20:12:32 +00:00
|
|
|
func contextWrite(ctxptr unsafe.Pointer, buffer unsafe.Pointer, length C.uint) C.int {
|
2015-09-17 21:22:07 +00:00
|
|
|
context := (*Context)(ctxptr)
|
|
|
|
|
2015-09-19 20:12:32 +00:00
|
|
|
written, err := context.writer.Write(C.GoBytes(buffer, C.int(length)))
|
2015-09-17 21:22:07 +00:00
|
|
|
if err != nil {
|
2015-09-19 20:12:32 +00:00
|
|
|
return C.int(-1)
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return C.int(written)
|
|
|
|
}
|