Exemplo n.º 1
0
// Eval executes the PHP expression contained in script, and returns a Value
// containing the PHP value returned by the expression, if any. Any output
// produced is written context's pre-defined io.Writer instance.
func (c *Context) Eval(script string) (*Value, error) {
	s := C.CString(script)
	defer C.free(unsafe.Pointer(s))

	result, err := C.context_eval(c.context, s)
	if err != nil {
		return nil, fmt.Errorf("Error executing script '%s' in context", script)
	}

	defer C.free(result)

	val, err := NewValueFromPtr(result)
	if err != nil {
		return nil, err
	}

	c.values = append(c.values, val)

	return val, nil
}
Exemplo n.º 2
0
// Eval executes the PHP expression contained in script, and returns a Value
// containing the PHP value returned by the expression, if any. Any output
// produced is written context's pre-defined io.Writer instance.
func (c *Context) Eval(script string) (*value.Value, error) {
	// When PHP compiles code with a non-NULL return value expected, it simply
	// prepends a `return` call to the code, thus breaking simple scripts that
	// would otherwise work. Thus, we need to wrap the code in a closure, and
	// call it immediately.
	s := C.CString("call_user_func(function(){" + script + "});")
	defer C.free(unsafe.Pointer(s))

	vptr, err := C.context_eval(c.context, s)
	if err != nil {
		return nil, fmt.Errorf("Error executing script '%s' in context", script)
	}

	val, err := value.NewFromPtr(vptr)
	if err != nil {
		return nil, err
	}

	return val, nil
}