func getTimeoutMsec(timeout_msec_ C.SEXP) (int, error) { if C.TYPEOF(timeout_msec_) != C.REALSXP || int(C.Rf_xlength(timeout_msec_)) != 1 { return 0, fmt.Errorf("getTimeoutMsec() error: timeout_msec must be a single number; it should convey the number of milliseconds to wait before timing out the call.") } return int(C.get_real_elt(timeout_msec_, 0)), nil }
//export ReadMsgpackFrame // // ReadMsgpackFrame reads the msgpack frame at byteOffset in rawStream, decodes the // 2-5 bytes of a msgpack binary array (either bin8, bin16, or bin32), and returns // and the decoded-into-R object and the next byteOffset to use. // func ReadMsgpackFrame(rawStream C.SEXP, byteOffset C.SEXP) C.SEXP { var start int if C.TYPEOF(byteOffset) == C.REALSXP { start = int(C.get_real_elt(byteOffset, 0)) } else if C.TYPEOF(byteOffset) == C.INTSXP { start = int(C.get_int_elt(byteOffset, 0)) } else { C.ReportErrorToR_NoReturn(C.CString("read.msgpack.frame(x, byteOffset) requires byteOffset to be a numeric byte-offset number.")) } // rawStream must be a RAWSXP if C.TYPEOF(rawStream) != C.RAWSXP { C.ReportErrorToR_NoReturn(C.CString("read.msgpack.frame(x, byteOffset) requires x be a RAW vector of bytes.")) } n := int(C.Rf_xlength(rawStream)) if n == 0 { return C.R_NilValue } if start >= n { C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("read.msgpack.frame(x, byteOffset) error: byteOffset(%d) is beyond the length of x (x has len %d).", start, n))) } var decoder [5]byte C.memcpy(unsafe.Pointer(&decoder[0]), unsafe.Pointer(C.get_raw_elt_ptr(rawStream, C.ulonglong(start))), C.size_t(5)) headerSz, _, totalSz, err := DecodeMsgpackBinArrayHeader(decoder[:]) if err != nil { C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("ReadMsgpackFrame error trying to decode msgpack frame: %s", err))) } if start+totalSz > n { C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("read.msgpack.frame(x, byteOffset) error: byteOffset(%d) plus the frames size(%d) goes beyond the length of x (x has len %d).", start, totalSz, n))) } bytes := make([]byte, totalSz) C.memcpy(unsafe.Pointer(&bytes[0]), unsafe.Pointer(C.get_raw_elt_ptr(rawStream, C.ulonglong(start))), C.size_t(totalSz)) rObject := decodeMsgpackToR(bytes[headerSz:]) C.Rf_protect(rObject) returnList := C.allocVector(C.VECSXP, C.R_xlen_t(2)) C.Rf_protect(returnList) C.SET_VECTOR_ELT(returnList, C.R_xlen_t(0), C.Rf_ScalarReal(C.double(float64(start+totalSz)))) C.SET_VECTOR_ELT(returnList, C.R_xlen_t(1), rObject) C.Rf_unprotect_ptr(rObject) C.Rf_unprotect_ptr(returnList) return returnList }
//export FromMsgpack func FromMsgpack(s C.SEXP) C.SEXP { // starting from within R, we convert a raw byte vector into R structures. // s must be a RAWSXP if C.TYPEOF(s) != C.RAWSXP { C.ReportErrorToR_NoReturn(C.CString("from.msgpack(x) requires x be a RAW vector of bytes.")) } n := int(C.Rf_xlength(s)) if n == 0 { return C.R_NilValue } bytes := make([]byte, n) C.memcpy(unsafe.Pointer(&bytes[0]), unsafe.Pointer(C.RAW(s)), C.size_t(n)) return decodeMsgpackToR(bytes) }
//export ReadNewlineDelimJson // // ReadNewlineDelimJson reads a json object at byteOffset in rawStream, expects // it to be newline terminated, and returns the // decoded-into-R object and the next byteOffset to use (the byte just after // the terminating newline). // func ReadNewlineDelimJson(rawStream C.SEXP, byteOffset C.SEXP) C.SEXP { C.Rf_protect(rawStream) var start int if C.TYPEOF(byteOffset) == C.REALSXP { start = int(C.get_real_elt(byteOffset, 0)) } else if C.TYPEOF(byteOffset) == C.INTSXP { start = int(C.get_int_elt(byteOffset, 0)) } else { C.ReportErrorToR_NoReturn(C.CString("read.ndjson(x, byteOffset) requires byteOffset to be a numeric byte-offset number.")) } // rawStream must be a RAWSXP if C.TYPEOF(rawStream) != C.RAWSXP { C.ReportErrorToR_NoReturn(C.CString("read.ndjson(x, byteOffset) requires x be a RAW vector of bytes.")) } n := int(C.Rf_xlength(rawStream)) if n == 0 { return C.R_NilValue } if start >= n { C.ReportErrorToR_NoReturn(C.CString(fmt.Sprintf("read.ndjson(x, byteOffset) error: byteOffset(%d) is at or beyond the length of x (x has len %d).", start, n))) } // INVAR: start < n // find the next newline or end of raw array next := int(C.next_newline_pos(rawStream, C.ulonglong(start+1), C.ulonglong(n))) totalSz := next - start bytes := make([]byte, totalSz) fromPtr := unsafe.Pointer(C.get_raw_elt_ptr(rawStream, C.ulonglong(start))) C.memcpy(unsafe.Pointer(&bytes[0]), fromPtr, C.size_t(totalSz)) rObject := decodeJsonToR(bytes) C.Rf_protect(rObject) returnList := C.allocVector(C.VECSXP, C.R_xlen_t(2)) C.Rf_protect(returnList) C.SET_VECTOR_ELT(returnList, C.R_xlen_t(0), C.Rf_ScalarReal(C.double(float64(start+totalSz)))) C.SET_VECTOR_ELT(returnList, C.R_xlen_t(1), rObject) C.Rf_unprotect_ptr(rObject) C.Rf_unprotect_ptr(returnList) C.Rf_unprotect_ptr(rawStream) return returnList }
func toIface(s C.SEXP) interface{} { // generate a go map or slice or scalar value, then encode it n := int(C.Rf_xlength(s)) if n == 0 { return nil // drops type info. Meh. } switch C.TYPEOF(s) { case C.VECSXP: // an R generic vector; e.g list() VPrintf("encodeRIntoMsgpack sees VECSXP\n") // could be a map or a slice. Check out the names. rnames := C.Rf_getAttrib(s, C.R_NamesSymbol) rnamesLen := int(C.Rf_xlength(rnames)) VPrintf("namesLen = %d\n", rnamesLen) if rnamesLen > 0 { myMap := map[string]interface{}{} for i := 0; i < rnamesLen; i++ { myMap[C.GoString(C.get_string_elt(rnames, C.int(i)))] = toIface(C.VECTOR_ELT(s, C.R_xlen_t(i))) } VPrintf("VECSXP myMap = '%#v'\n", myMap) return myMap } else { // else: no names, so we treat it as an array instead of as a map mySlice := make([]interface{}, n) for i := 0; i < n; i++ { mySlice[i] = toIface(C.VECTOR_ELT(s, C.R_xlen_t(i))) } VPrintf("VECSXP mySlice = '%#v'\n", mySlice) return mySlice } case C.REALSXP: // a vector of float64 (numeric) VPrintf("encodeRIntoMsgpack sees REALSXP\n") mySlice := make([]float64, n) for i := 0; i < n; i++ { mySlice[i] = float64(C.get_real_elt(s, C.int(i))) } VPrintf("VECSXP mySlice = '%#v'\n", mySlice) return mySlice case C.INTSXP: // a vector of int32 VPrintf("encodeRIntoMsgpack sees INTSXP\n") mySlice := make([]int, n) for i := 0; i < n; i++ { mySlice[i] = int(C.get_int_elt(s, C.int(i))) } VPrintf("INTSXP mySlice = '%#v'\n", mySlice) return mySlice case C.RAWSXP: VPrintf("encodeRIntoMsgpack sees RAWSXP\n") mySlice := make([]byte, n) C.memcpy(unsafe.Pointer(&mySlice[0]), unsafe.Pointer(C.RAW(s)), C.size_t(n)) VPrintf("RAWSXP mySlice = '%#v'\n", mySlice) return mySlice case C.STRSXP: // a vector of string (pointers to charsxp that are interned) VPrintf("encodeRIntoMsgpack sees STRSXP\n") mySlice := make([]string, n) for i := 0; i < n; i++ { mySlice[i] = C.GoString(C.get_string_elt(s, C.int(i))) } VPrintf("STRSXP mySlice = '%#v'\n", mySlice) return mySlice case C.NILSXP: // c(); an empty vector VPrintf("encodeRIntoMsgpack sees NILSXP\n") return nil case C.CHARSXP: // a single string, interned in a global pool for reuse by STRSXP. VPrintf("encodeRIntoMsgpack sees CHARSXP\n") case C.SYMSXP: VPrintf("encodeRIntoMsgpack sees SYMSXP\n") case C.LISTSXP: VPrintf("encodeRIntoMsgpack sees LISTSXP\n") case C.CLOSXP: VPrintf("encodeRIntoMsgpack sees CLOSXP\n") case C.ENVSXP: VPrintf("encodeRIntoMsgpack sees ENVSXP\n") case C.PROMSXP: VPrintf("encodeRIntoMsgpack sees PROMSXP\n") case C.LANGSXP: VPrintf("encodeRIntoMsgpack sees LANGSXP\n") case C.SPECIALSXP: VPrintf("encodeRIntoMsgpack sees SPECIALSXP\n") case C.BUILTINSXP: VPrintf("encodeRIntoMsgpack sees BUILTINSXP\n") case C.LGLSXP: VPrintf("encodeRIntoMsgpack sees LGLSXP\n") case C.CPLXSXP: VPrintf("encodeRIntoMsgpack sees CPLXSXP\n") case C.DOTSXP: VPrintf("encodeRIntoMsgpack sees DOTSXP\n") case C.ANYSXP: VPrintf("encodeRIntoMsgpack sees ANYSXP\n") case C.EXPRSXP: VPrintf("encodeRIntoMsgpack sees EXPRSXP\n") case C.BCODESXP: VPrintf("encodeRIntoMsgpack sees BCODESXP\n") case C.EXTPTRSXP: VPrintf("encodeRIntoMsgpack sees EXTPTRSXP\n") case C.WEAKREFSXP: VPrintf("encodeRIntoMsgpack sees WEAKREFSXP\n") case C.S4SXP: VPrintf("encodeRIntoMsgpack sees S4SXP\n") default: VPrintf("encodeRIntoMsgpack sees <unknown>\n") } VPrintf("... warning: encodeRIntoMsgpack() ignoring this input.\n") return nil }