diff --git a/ph7.go b/ph7.go index 53ebe00..560dac2 100644 --- a/ph7.go +++ b/ph7.go @@ -155,8 +155,6 @@ func (e *Engine) CompileFile(path string, phpOnly bool) (*VM, error) { return newVM(cvm), nil } -// TODO (*Engine).CompileFile - func (e *Engine) Close() error { return newError(C.ph7_release((*C.ph7)(e))) } @@ -203,6 +201,7 @@ func (c *Context) ResultValue(value *Value) error { // TODO (*Context).ResultResource func (c *Context) Output(s string) error { + // TODO Replace this with Write([]byte) (int, bool). cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) @@ -216,7 +215,17 @@ func (c *Context) RandomNum() uint { return uint(C.ph7_context_random_num((*C.ph7_context)(c))) } -// TODO (*Context).RandomString +func (c *Context) RandomString(length int) (string, error) { + buf := (*C.char)(C.malloc(C.size_t(length))) + defer C.free(unsafe.Pointer(buf)) + + err := newError(C.ph7_context_random_string((*C.ph7_context)(c), nil, C.int(length))) + if err != nil { + return "", err + } + return C.GoStringN(buf, C.int(length)), nil +} + // TODO (*Context).UserData // TODO (*Context).PushAuxData // TODO (*Context).PeekAuxData @@ -230,13 +239,37 @@ func (c *Context) RandomNum() uint { type Value C.ph7_value -// TODO (*Value).ToInt -// TODO (*Value).ToBool -// TODO (*Value).ToInt64 -// TODO (*Value).ToDouble -// TODO (*Value).ToString +func (v *Value) ToInt() int { + return int(C.ph7_value_to_int((*C.ph7_value)(v))) +} + +func (v *Value) ToBool() bool { + return C.ph7_value_to_bool((*C.ph7_value)(v)) != 0 +} + +func (v *Value) ToInt64() int64 { + return int64(C.ph7_value_to_int64((*C.ph7_value)(v))) +} + +func (v *Value) ToDouble() float64 { + return float64(C.ph7_value_to_double((*C.ph7_value)(v))) +} + +func (v *Value) ToString() string { + var strLen C.int + cstr := C.ph7_value_to_string((*C.ph7_value)(v), &strLen) + return C.GoStringN(cstr, strLen) +} + // TODO (*Value).ToResource -// TODO (*Value).Compare + +func (v *Value) Compare(v2 *Value) int { + return int(C.ph7_value_compare((*C.ph7_value)(v), (*C.ph7_value)(v2), 0)) +} + +func (v *Value) CompareStrict(v2 *Value) int { + return int(C.ph7_value_compare((*C.ph7_value)(v), (*C.ph7_value)(v2), 1)) +} // TODO (*Value).Int // TODO (*Value).Int64