Wrapped some more context and value methods

This commit is contained in:
Brandon Dyck 2022-11-27 14:51:01 -07:00
parent e37a792176
commit 92c0f98520

51
ph7.go
View File

@ -155,8 +155,6 @@ func (e *Engine) CompileFile(path string, phpOnly bool) (*VM, error) {
return newVM(cvm), nil return newVM(cvm), nil
} }
// TODO (*Engine).CompileFile
func (e *Engine) Close() error { func (e *Engine) Close() error {
return newError(C.ph7_release((*C.ph7)(e))) return newError(C.ph7_release((*C.ph7)(e)))
} }
@ -203,6 +201,7 @@ func (c *Context) ResultValue(value *Value) error {
// TODO (*Context).ResultResource // TODO (*Context).ResultResource
func (c *Context) Output(s string) error { func (c *Context) Output(s string) error {
// TODO Replace this with Write([]byte) (int, bool).
cs := C.CString(s) cs := C.CString(s)
defer C.free(unsafe.Pointer(cs)) 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))) 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).UserData
// TODO (*Context).PushAuxData // TODO (*Context).PushAuxData
// TODO (*Context).PeekAuxData // TODO (*Context).PeekAuxData
@ -230,13 +239,37 @@ func (c *Context) RandomNum() uint {
type Value C.ph7_value type Value C.ph7_value
// TODO (*Value).ToInt func (v *Value) ToInt() int {
// TODO (*Value).ToBool return int(C.ph7_value_to_int((*C.ph7_value)(v)))
// TODO (*Value).ToInt64 }
// TODO (*Value).ToDouble
// TODO (*Value).ToString 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).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).Int
// TODO (*Value).Int64 // TODO (*Value).Int64