Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for Tcl 9 and ensemble fixes. #46

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
51 changes: 34 additions & 17 deletions generic/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,17 @@ int JSON_JArrayObjGetElements(Tcl_Interp* interp, Tcl_Obj* arrayObj, int* objc,
{
enum json_types type;
Tcl_Obj* val = NULL;
Tcl_Size count;

TEST_OK(JSON_GetJvalFromObj(interp, arrayObj, &type, &val));
if (type != JSON_ARRAY) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("Expecting a JSON array, but got a JSON %s", get_type_name(type)));
return TCL_ERROR;
}
TEST_OK(Tcl_ListObjGetElements(interp, val, objc, objv));
TEST_OK(Tcl_ListObjGetElements(interp, val, &count, objv));
if (objc != NULL) {
*objc = count;
}

return TCL_OK;
}
Expand Down Expand Up @@ -392,7 +396,7 @@ int JSON_Extract(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj* path, Tcl_Obj** res)
Tcl_Obj* target = NULL;
Tcl_Obj** pathv = NULL;
Tcl_Obj* def = NULL;
int pathc = 0;
Tcl_Size pathc = 0;

if (path)
TEST_OK_LABEL(finally, code, Tcl_ListObjGetElements(interp, path, &pathc, &pathv));
Expand All @@ -418,7 +422,7 @@ int JSON_Exists(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj* path, int* exists) //
{
Tcl_Obj* target = NULL;
Tcl_Obj** pathv = NULL;
int pathc = 0;
Tcl_Size pathc = 0;

if (path)
TEST_OK(Tcl_ListObjGetElements(interp, path, &pathc, &pathv));
Expand Down Expand Up @@ -451,7 +455,7 @@ int JSON_Set(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj *path, Tcl_Obj* replaceme
Tcl_Obj* newval;
Tcl_Obj* rep = NULL;
Tcl_Obj** pathv = NULL;
int pathc = 0;
Tcl_Size pathc = 0;

if (Tcl_IsShared(obj))
THROW_ERROR_LABEL(finally, code, "JSON_Set called with shared object");
Expand Down Expand Up @@ -498,7 +502,8 @@ int JSON_Set(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj *path, Tcl_Obj* replaceme
//}}}
case JSON_ARRAY: //{{{
{
int ac, index_str_len, ok=1;
Tcl_Size ac, index_str_len;
int ok=1;
long index;
const char* index_str;
char* end;
Expand Down Expand Up @@ -658,7 +663,7 @@ int JSON_Unset(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj *path) //{{{
Tcl_Obj* step = NULL;
Tcl_Obj* src = NULL;
Tcl_Obj* target = NULL;
int pathc = 0;
Tcl_Size pathc = 0;
Tcl_Obj** pathv = NULL;
int retval = TCL_OK;

Expand Down Expand Up @@ -711,7 +716,8 @@ int JSON_Unset(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj *path) //{{{
//}}}
case JSON_ARRAY: //{{{
{
int ac, index_str_len, ok=1;
Tcl_Size ac, index_str_len;
int ok=1;
long index;
const char* index_str;
char* end;
Expand Down Expand Up @@ -809,7 +815,8 @@ int JSON_Unset(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj *path) //{{{
//}}}
case JSON_ARRAY: //{{{
{
int ac, index_str_len, ok=1;
Tcl_Size ac, index_str_len;
int ok=1;
long index;
const char* index_str;
char* end;
Expand Down Expand Up @@ -1030,15 +1037,21 @@ int JSON_Length(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_Obj* path, int* length) //
int retval = TCL_OK;
Tcl_Obj* val = NULL;
Tcl_Obj* target = NULL;
Tcl_Size len;

TEST_OK_LABEL(finally, retval, JSON_Extract(interp, obj, path, &target));

TEST_OK_LABEL(finally, retval, JSON_GetJvalFromObj(interp, target, &type, &val));

switch (type) {
case JSON_ARRAY: retval = Tcl_ListObjLength(interp, val, length); break;
case JSON_OBJECT: retval = Tcl_DictObjSize(interp, val, length); break;

case JSON_ARRAY:
retval = Tcl_ListObjLength(interp, val, &len);
if (length != NULL) *length = len;
break;
case JSON_OBJECT:
retval = Tcl_DictObjSize(interp, val, &len);
if (length != NULL) *length = len;
break;
case JSON_DYN_STRING:
case JSON_DYN_NUMBER:
case JSON_DYN_BOOL:
Expand Down Expand Up @@ -1126,7 +1139,7 @@ int JSON_Foreach(Tcl_Interp* interp, Tcl_Obj* iterators, JSON_ForeachBody* body,
unsigned int i;
int retcode=TCL_OK;
struct foreach_state* state = NULL;
int objc;
Tcl_Size objc;
Tcl_Obj** objv = NULL;
Tcl_Obj* it_res = NULL;
struct interp_cx* l = Tcl_GetAssocData(interp, "rl_json", NULL);
Expand Down Expand Up @@ -1173,14 +1186,16 @@ int JSON_Foreach(Tcl_Interp* interp, Tcl_Obj* iterators, JSON_ForeachBody* body,
}

for (i=0; i<state->iterators; i++) {
int loops, j;
Tcl_Size loops, count;
int j;
enum json_types type;
Tcl_Obj* val = NULL;
Tcl_Obj* varlist = objv[i*2];

replace_tclobj(&state->it[i].varlist, varlist);

TEST_OK_LABEL(done, retcode, Tcl_ListObjGetElements(interp, state->it[i].varlist, &state->it[i].var_c, &state->it[i].var_v));
TEST_OK_LABEL(done, retcode, Tcl_ListObjGetElements(interp, state->it[i].varlist, &count, &state->it[i].var_v));
state->it[i].var_c = count;
for (j=0; j < state->it[i].var_c; j++)
Tcl_IncrRefCount(state->it[i].var_v[j]);

Expand All @@ -1191,7 +1206,8 @@ int JSON_Foreach(Tcl_Interp* interp, Tcl_Obj* iterators, JSON_ForeachBody* body,
switch (type) {
case JSON_ARRAY:
TEST_OK_LABEL(done, retcode,
Tcl_ListObjGetElements(interp, val, &state->it[i].data_c, &state->it[i].data_v));
Tcl_ListObjGetElements(interp, val, &count, &state->it[i].data_v));
state->it[i].data_c = count;
state->it[i].data_i = 0;
state->it[i].is_array = 1;
loops = (int)ceil(state->it[i].data_c / (double)state->it[i].var_c);
Expand Down Expand Up @@ -1318,7 +1334,8 @@ int JSON_Foreach(Tcl_Interp* interp, Tcl_Obj* iterators, JSON_ForeachBody* body,
break;
//}}}
} else { // Iterate over it_res as a list {{{
int oc, i;
Tcl_Size oc;
int i;
Tcl_Obj** ov = NULL;

TEST_OK_LABEL(done, retcode, Tcl_ListObjGetElements(interp, it_res, &oc, &ov));
Expand Down Expand Up @@ -1385,7 +1402,7 @@ int JSON_Valid(Tcl_Interp* interp, Tcl_Obj* json, int* valid, enum extensions ex
const unsigned char* p;
const unsigned char* e;
const unsigned char* val_start;
int len;
Tcl_Size len;
struct parse_context cx[CX_STACK_SIZE];

if (interp)
Expand Down
17 changes: 9 additions & 8 deletions generic/cbor.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "rl_jsonInt.h"
#include <limits.h>

enum svalue_types {
S_FALSE = 20,
Expand Down Expand Up @@ -519,7 +520,7 @@ static int cbor_match_map(Tcl_Interp* interp, uint8_t ai, uint64_t val, const ui
Tcl_Obj* cbor_val = NULL;
Tcl_HashTable remaining;
const uint8_t* p = *pPtr;
int size;
Tcl_Size size;
int skipping = 0;

Tcl_InitHashTable(&remaining, TCL_ONE_WORD_KEYS);
Expand Down Expand Up @@ -709,7 +710,7 @@ static int cbor_matches(Tcl_Interp* interp, const uint8_t** pPtr, const uint8_t*
//}}}
case M_BSTR: // Compare as byte strings {{{
{
size_t pathlen;
Tcl_Size pathlen;
const uint8_t* pathval = (const uint8_t*)Tcl_GetBytesFromObj(interp, pathElem, &pathlen);
const uint8_t*const pathend = pathval + pathlen;
const uint8_t*const pe = p + val;
Expand Down Expand Up @@ -768,7 +769,7 @@ static int cbor_matches(Tcl_Interp* interp, const uint8_t** pPtr, const uint8_t*
//}}}
case M_UTF8: // Compare as UTF-8 strings {{{
{
int s_pathlen;
Tcl_Size s_pathlen;
const uint8_t* s_pathval = (const uint8_t*)Tcl_GetStringFromObj(pathElem, &s_pathlen);
const uint8_t*const s_pathend = s_pathval + s_pathlen;
const uint8_t*const s_pe = p + val;
Expand Down Expand Up @@ -837,7 +838,7 @@ static int cbor_matches(Tcl_Interp* interp, const uint8_t** pPtr, const uint8_t*
//}}}
case M_ARR: // Compare as a list {{{
{
int oc;
Tcl_Size oc;
Tcl_Obj** ov;
if (TCL_OK != Tcl_ListObjGetElements(NULL, pathElem, &oc, &ov)) {
// Skip remaining elements {{{
Expand Down Expand Up @@ -919,7 +920,7 @@ static int cbor_matches(Tcl_Interp* interp, const uint8_t** pPtr, const uint8_t*
}
case 22: case 23: // Simple value: null / undefined - treat zero length string as matching
{
int len;
Tcl_Size len;
Tcl_GetStringFromObj(pathElem, &len);
if (len == 0) goto matches;
goto mismatch;
Expand Down Expand Up @@ -959,9 +960,9 @@ int CBOR_GetDataItemFromPath(Tcl_Interp* interp, Tcl_Obj* cborObj, Tcl_Obj* path
{
int code = TCL_OK;
Tcl_Obj** pathv = NULL;
int pathc = 0;
Tcl_Size pathc = 0;
const uint8_t* p = NULL;
size_t byteslen = 0;
Tcl_Size byteslen = 0;
const uint8_t* bytes = NULL;
const uint8_t** circular = g_circular_buf;

Expand Down Expand Up @@ -1253,7 +1254,7 @@ static int cbor_nr_cmd(ClientData cdata, Tcl_Interp* interp, int objc, Tcl_Obj*c
enum {A_cmd=A_OP, A_BYTES, A_objc};
CHECK_ARGS_LABEL(finally, code, "bytes");

int len;
Tcl_Size len;
const uint8_t* bytes = Tcl_GetByteArrayFromObj(objv[A_BYTES], &len);
const uint8_t* p = bytes;

Expand Down
10 changes: 5 additions & 5 deletions generic/json_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ int JSON_SetIntRep(Tcl_Obj* target, enum json_types type, Tcl_Obj* replacement)
replace_tclobj(&rep, replacement);

if (type == JSON_STRING && rep) { // Check for template values
int len;
Tcl_Size len;
const char* str = Tcl_GetStringFromObj(replacement, &len);
const char*const strend = str + len;
enum json_types template_type;
Expand Down Expand Up @@ -412,7 +412,7 @@ static void dup_internal_rep(Tcl_Obj* src, Tcl_Obj* dest, Tcl_ObjType* objtype)
Tcl_Panic("dup_internal_rep asked to duplicate for type, but that type wasn't available on the src object");

if (src == srcir->twoPtrValue.ptr1) {
int len;
Tcl_Size len;
const char* str = Tcl_GetStringFromObj((Tcl_Obj*)srcir->twoPtrValue.ptr1, &len);
// Don't know how this happens yet, but it's bad news - we get into an endless recursion of duplicateobj calls until the stack blows up

Expand All @@ -421,7 +421,7 @@ static void dup_internal_rep(Tcl_Obj* src, Tcl_Obj* dest, Tcl_ObjType* objtype)
} else {
if (objtype == &json_array) {
Tcl_Obj** ov = NULL;
int oc;
Tcl_Size oc;
// The list type's internal structure sharing on duplicates messes up our sharing,
// rather recreate a fresh list referencing the original element objects instead
if (TCL_OK != Tcl_ListObjGetElements(NULL, srcir->twoPtrValue.ptr1, &oc, &ov))
Expand Down Expand Up @@ -491,7 +491,7 @@ static void update_string_rep_number(Tcl_Obj* obj) //{{{
{
Tcl_ObjInternalRep* ir = Tcl_FetchInternalRep(obj, &json_number);
const char* str;
int len;
Tcl_Size len;

if (ir->twoPtrValue.ptr1 == obj)
Tcl_Panic("Turtles all the way down!");
Expand Down Expand Up @@ -565,7 +565,7 @@ static int set_from_any(Tcl_Interp* interp, Tcl_Obj* obj, Tcl_ObjType** objtype,
const unsigned char* p;
const unsigned char* e;
const unsigned char* val_start;
int len;
Tcl_Size len;
struct parse_context cx[CX_STACK_SIZE];
enum extensions extensions = EXT_COMMENTS;
struct parse_error details = {0};
Expand Down
Loading