summaryrefslogtreecommitdiff
path: root/upb/json
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2019-01-12 19:12:57 -0800
committerJoshua Haberman <jhaberman@gmail.com>2019-01-12 19:12:57 -0800
commit48863ea0be94ea3d3d61206ad7ce9ead206770fa (patch)
tree9c4b647586ecb7c52fa56f8cef26301027ec5ae7 /upb/json
parentd2f9bec5c6f3c34362cf13e35e11d3dbc7888a32 (diff)
A lot more tests are working now.
Diffstat (limited to 'upb/json')
-rw-r--r--upb/json/parser.c144
-rw-r--r--upb/json/parser.h140
-rw-r--r--upb/json/parser.rl38
-rw-r--r--upb/json/printer.c32
-rw-r--r--upb/json/printer.h68
5 files changed, 217 insertions, 205 deletions
diff --git a/upb/json/parser.c b/upb/json/parser.c
index 4bc9163..1dac800 100644
--- a/upb/json/parser.c
+++ b/upb/json/parser.c
@@ -301,13 +301,13 @@ static void json_parser_any_frame_set_payload_type(
/* Initialize encoder. */
h = upb_handlercache_get(frame->encoder_handlercache, payload_type);
- encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink);
+ encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink);
/* Initialize parser. */
parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type);
upb_sink_reset(&frame->sink, h, encoder);
frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab,
- &frame->sink, p->ignore_json_unknown);
+ frame->sink, p->ignore_json_unknown);
}
static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) {
@@ -383,9 +383,12 @@ static bool check_stack(upb_json_parser *p) {
static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
upb_value v;
const upb_json_codecache *cache = p->method->cache;
- bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
- const upb_json_parsermethod *method = upb_value_getptr(v);
+ bool ok;
+ const upb_json_parsermethod *method;
+
+ ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
UPB_ASSERT(ok);
+ method = upb_value_getconstptr(v);
frame->name_table = &method->name_table;
}
@@ -1287,7 +1290,7 @@ static bool end_any_stringval(upb_json_parser *p) {
}
json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type);
-
+
return true;
} else {
upb_status_seterrf(
@@ -2416,11 +2419,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) {
* final state once, when the closing '"' is seen. */
-#line 2578 "upb/json/parser.rl"
+#line 2581 "upb/json/parser.rl"
-#line 2424 "upb/json/parser.c"
+#line 2427 "upb/json/parser.c"
static const char _json_actions[] = {
0, 1, 0, 1, 1, 1, 3, 1,
4, 1, 6, 1, 7, 1, 8, 1,
@@ -2667,7 +2670,7 @@ static const int json_en_value_machine = 75;
static const int json_en_main = 1;
-#line 2581 "upb/json/parser.rl"
+#line 2584 "upb/json/parser.rl"
size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const upb_bufhandle *handle) {
@@ -2690,7 +2693,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size,
capture_resume(parser, buf);
-#line 2694 "upb/json/parser.c"
+#line 2697 "upb/json/parser.c"
{
int _klen;
unsigned int _trans;
@@ -2765,83 +2768,83 @@ _match:
switch ( *_acts++ )
{
case 1:
-#line 2429 "upb/json/parser.rl"
+#line 2432 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 2:
-#line 2431 "upb/json/parser.rl"
+#line 2434 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 23;goto _again;} }
break;
case 3:
-#line 2435 "upb/json/parser.rl"
+#line 2438 "upb/json/parser.rl"
{ start_text(parser, p); }
break;
case 4:
-#line 2436 "upb/json/parser.rl"
+#line 2439 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_text(parser, p)); }
break;
case 5:
-#line 2442 "upb/json/parser.rl"
+#line 2445 "upb/json/parser.rl"
{ start_hex(parser); }
break;
case 6:
-#line 2443 "upb/json/parser.rl"
+#line 2446 "upb/json/parser.rl"
{ hexdigit(parser, p); }
break;
case 7:
-#line 2444 "upb/json/parser.rl"
+#line 2447 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_hex(parser)); }
break;
case 8:
-#line 2450 "upb/json/parser.rl"
+#line 2453 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(escape(parser, p)); }
break;
case 9:
-#line 2456 "upb/json/parser.rl"
+#line 2459 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 10:
-#line 2468 "upb/json/parser.rl"
+#line 2471 "upb/json/parser.rl"
{ start_duration_base(parser, p); }
break;
case 11:
-#line 2469 "upb/json/parser.rl"
+#line 2472 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_duration_base(parser, p)); }
break;
case 12:
-#line 2471 "upb/json/parser.rl"
+#line 2474 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 13:
-#line 2476 "upb/json/parser.rl"
+#line 2479 "upb/json/parser.rl"
{ start_timestamp_base(parser, p); }
break;
case 14:
-#line 2477 "upb/json/parser.rl"
+#line 2480 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_base(parser, p)); }
break;
case 15:
-#line 2479 "upb/json/parser.rl"
+#line 2482 "upb/json/parser.rl"
{ start_timestamp_fraction(parser, p); }
break;
case 16:
-#line 2480 "upb/json/parser.rl"
+#line 2483 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); }
break;
case 17:
-#line 2482 "upb/json/parser.rl"
+#line 2485 "upb/json/parser.rl"
{ start_timestamp_zone(parser, p); }
break;
case 18:
-#line 2483 "upb/json/parser.rl"
+#line 2486 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); }
break;
case 19:
-#line 2485 "upb/json/parser.rl"
+#line 2488 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 20:
-#line 2490 "upb/json/parser.rl"
+#line 2493 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) {
{stack[top++] = cs; cs = 47;goto _again;}
@@ -2853,11 +2856,11 @@ _match:
}
break;
case 21:
-#line 2501 "upb/json/parser.rl"
+#line 2504 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 75;goto _again;} }
break;
case 22:
-#line 2506 "upb/json/parser.rl"
+#line 2509 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
start_any_member(parser, p);
@@ -2867,11 +2870,11 @@ _match:
}
break;
case 23:
-#line 2513 "upb/json/parser.rl"
+#line 2516 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_membername(parser)); }
break;
case 24:
-#line 2516 "upb/json/parser.rl"
+#line 2519 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
end_any_member(parser, p);
@@ -2881,7 +2884,7 @@ _match:
}
break;
case 25:
-#line 2527 "upb/json/parser.rl"
+#line 2530 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
start_any_object(parser, p);
@@ -2891,7 +2894,7 @@ _match:
}
break;
case 26:
-#line 2536 "upb/json/parser.rl"
+#line 2539 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
CHECK_RETURN_TOP(end_any_object(parser, p));
@@ -2901,54 +2904,54 @@ _match:
}
break;
case 27:
-#line 2548 "upb/json/parser.rl"
+#line 2551 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_array(parser)); }
break;
case 28:
-#line 2552 "upb/json/parser.rl"
+#line 2555 "upb/json/parser.rl"
{ end_array(parser); }
break;
case 29:
-#line 2557 "upb/json/parser.rl"
+#line 2560 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_number(parser, p)); }
break;
case 30:
-#line 2558 "upb/json/parser.rl"
+#line 2561 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); }
break;
case 31:
-#line 2560 "upb/json/parser.rl"
+#line 2563 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_stringval(parser)); }
break;
case 32:
-#line 2561 "upb/json/parser.rl"
+#line 2564 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_stringval(parser)); }
break;
case 33:
-#line 2563 "upb/json/parser.rl"
+#line 2566 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); }
break;
case 34:
-#line 2565 "upb/json/parser.rl"
+#line 2568 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); }
break;
case 35:
-#line 2567 "upb/json/parser.rl"
+#line 2570 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_null(parser)); }
break;
case 36:
-#line 2569 "upb/json/parser.rl"
+#line 2572 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_subobject_full(parser)); }
break;
case 37:
-#line 2570 "upb/json/parser.rl"
+#line 2573 "upb/json/parser.rl"
{ end_subobject_full(parser); }
break;
case 38:
-#line 2575 "upb/json/parser.rl"
+#line 2578 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
-#line 2952 "upb/json/parser.c"
+#line 2955 "upb/json/parser.c"
}
}
@@ -2965,32 +2968,32 @@ _again:
while ( __nacts-- > 0 ) {
switch ( *__acts++ ) {
case 0:
-#line 2427 "upb/json/parser.rl"
+#line 2430 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; if ( p == pe )
goto _test_eof;
goto _again;} }
break;
case 30:
-#line 2558 "upb/json/parser.rl"
+#line 2561 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); }
break;
case 33:
-#line 2563 "upb/json/parser.rl"
+#line 2566 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); }
break;
case 34:
-#line 2565 "upb/json/parser.rl"
+#line 2568 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); }
break;
case 35:
-#line 2567 "upb/json/parser.rl"
+#line 2570 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_null(parser)); }
break;
case 37:
-#line 2570 "upb/json/parser.rl"
+#line 2573 "upb/json/parser.rl"
{ end_subobject_full(parser); }
break;
-#line 2994 "upb/json/parser.c"
+#line 2997 "upb/json/parser.c"
}
}
}
@@ -2998,7 +3001,7 @@ goto _again;} }
_out: {}
}
-#line 2603 "upb/json/parser.rl"
+#line 2606 "upb/json/parser.rl"
if (p != pe) {
upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p);
@@ -3046,13 +3049,13 @@ static void json_parser_reset(upb_json_parser *p) {
/* Emit Ragel initialization of the parser. */
-#line 3050 "upb/json/parser.c"
+#line 3053 "upb/json/parser.c"
{
cs = json_start;
top = 0;
}
-#line 2650 "upb/json/parser.rl"
+#line 2653 "upb/json/parser.rl"
p->current_state = cs;
p->parser_top = top;
accumulate_clear(p);
@@ -3067,7 +3070,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_msg_field_iter i;
upb_alloc *alloc = upb_arena_alloc(&c->arena);
- upb_json_parsermethod *m = upb_gmalloc(sizeof(*m));
+ upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
m->cache = c;
@@ -3090,7 +3093,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
size_t len = upb_fielddef_getjsonname(f, NULL, 0);
buf = upb_malloc(alloc, len);
upb_fielddef_getjsonname(f, buf, len);
- upb_strtable_insert3(&m->name_table, buf, len, v, alloc);
+ upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc);
if (strcmp(buf, upb_fielddef_name(f)) != 0) {
/* Since the JSON name is different from the regular field name, add an
@@ -3109,7 +3112,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_json_parser *upb_json_parser_create(upb_env *env,
const upb_json_parsermethod *method,
const upb_symtab* symtab,
- upb_sink *output,
+ upb_sink output,
bool ignore_json_unknown) {
#ifndef NDEBUG
const size_t size_before = upb_env_bytesallocated(env);
@@ -3125,8 +3128,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
upb_bytessink_reset(&p->input_, &method->input_handler_, p);
json_parser_reset(p);
- upb_sink_reset(&p->top->sink, output->handlers, output->closure);
- p->top->m = upb_handlers_msgdef(output->handlers);
+ p->top->sink = output;
+ p->top->m = upb_handlers_msgdef(output.handlers);
if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
p->top->is_any = true;
p->top->any_frame = json_parser_any_frame_new(p);
@@ -3146,8 +3149,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
return p;
}
-upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
- return &p->input_;
+upb_bytessink upb_json_parser_input(upb_json_parser *p) {
+ return p->input_;
}
const upb_byteshandler *upb_json_parsermethod_inputhandler(
@@ -3174,21 +3177,22 @@ void upb_json_codecache_free(upb_json_codecache *c) {
upb_gfree(c);
}
-upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
- const upb_msgdef *md) {
+const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
+ const upb_msgdef *md) {
upb_json_parsermethod *m;
upb_value v;
upb_msg_field_iter i;
+ upb_alloc *alloc = upb_arena_alloc(&c->arena);
if (upb_inttable_lookupptr(&c->methods, md, &v)) {
- return upb_value_getptr(v);
+ return upb_value_getconstptr(v);
}
m = parsermethod_new(c, md);
- v = upb_value_ptr(m);
+ v = upb_value_constptr(m);
if (!m) return NULL;
- if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL;
+ if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL;
/* Populate parser methods for all submessages, so the name tables will
* be available during parsing. */
diff --git a/upb/json/parser.h b/upb/json/parser.h
index d5ec396..d1a1471 100644
--- a/upb/json/parser.h
+++ b/upb/json/parser.h
@@ -14,17 +14,44 @@
namespace upb {
namespace json {
class CodeCache;
-class Parser;
-class ParserMethod;
+class ParserPtr;
+class ParserMethodPtr;
} /* namespace json */
} /* namespace upb */
#endif
-UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser)
-UPB_DECLARE_TYPE(upb::json::ParserMethod, upb_json_parsermethod)
-UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache)
+/* upb_json_parsermethod ******************************************************/
-/* upb::json::Parser **********************************************************/
+struct upb_json_parsermethod;
+typedef struct upb_json_parsermethod upb_json_parsermethod;
+
+UPB_BEGIN_EXTERN_C
+
+const upb_byteshandler* upb_json_parsermethod_inputhandler(
+ const upb_json_parsermethod* m);
+
+UPB_END_EXTERN_C
+
+#ifdef __cplusplus
+
+class upb::json::ParserMethodPtr {
+ public:
+ ParserMethodPtr() : ptr_(nullptr) {}
+ ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {}
+
+ const upb_json_parsermethod* ptr() const { return ptr_; }
+
+ const BytesHandler* input_handler() const {
+ return upb_json_parsermethod_inputhandler(ptr());
+ }
+
+ private:
+ const upb_json_parsermethod* ptr_;
+};
+
+#endif /* __cplusplus */
+
+/* upb_json_parser ************************************************************/
/* Preallocation hint: parser won't allocate more bytes than this when first
* constructed. This hint may be an overestimate for some build configurations.
@@ -32,89 +59,72 @@ UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache)
* it may be an underestimate. */
#define UPB_JSON_PARSER_SIZE 5712
+struct upb_json_parser;
+typedef struct upb_json_parser upb_json_parser;
+
+UPB_BEGIN_EXTERN_C
+
+upb_json_parser*
+upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m,
+ const upb_symtab* symtab, upb_sink output,
+ bool ignore_json_unknown);
+upb_bytessink upb_json_parser_input(upb_json_parser* p);
+
+UPB_END_EXTERN_C
+
#ifdef __cplusplus
/* Parses an incoming BytesStream, pushing the results to the destination
* sink. */
-class upb::json::Parser {
+class upb::json::ParserPtr {
public:
- static Parser* Create(Environment* env, const ParserMethod* method,
- const SymbolTable* symtab,
- Sink* output, bool ignore_json_unknown);
+ ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {}
- BytesSink* input();
+ static ParserPtr Create(Environment* env, ParserMethodPtr method,
+ SymbolTable* symtab, Sink output,
+ bool ignore_json_unknown) {
+ upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr;
+ return ParserPtr(upb_json_parser_create(
+ env, method.ptr(), symtab_ptr, output.sink(), ignore_json_unknown));
+ }
- private:
- UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser)
-};
-
-class upb::json::ParserMethod {
- public:
- /* The input handlers for this decoder method. */
- const BytesHandler* input_handler() const;
+ BytesSink input() { return upb_json_parser_input(ptr_); }
private:
- UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod)
+ upb_json_parser* ptr_;
};
-class upb::json::CodeCache {
- public:
- static CodeCache* New();
- static void Free(CodeCache* cache);
-
- /* Returns a DecoderMethod that can push data to the given handlers.
- * If a suitable method already exists, it will be returned from the cache. */
- const ParserMethod *Get(const MessageDef* md);
+#endif /* __cplusplus */
- private:
- UPB_DISALLOW_POD_OPS(CodeCache, upb::json::CodeCache)
-};
+/* upb_json_codecache *********************************************************/
-#endif
+struct upb_json_codecache;
+typedef struct upb_json_codecache upb_json_codecache;
UPB_BEGIN_EXTERN_C
-upb_json_parser* upb_json_parser_create(upb_env* e,
- const upb_json_parsermethod* m,
- const upb_symtab* symtab,
- upb_sink* output,
- bool ignore_json_unknown);
-upb_bytessink *upb_json_parser_input(upb_json_parser *p);
-
-const upb_byteshandler *upb_json_parsermethod_inputhandler(
- const upb_json_parsermethod *m);
-
upb_json_codecache *upb_json_codecache_new();
void upb_json_codecache_free(upb_json_codecache *cache);
-upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
- const upb_msgdef* md);
+const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
+ const upb_msgdef* md);
UPB_END_EXTERN_C
#ifdef __cplusplus
-namespace upb {
-namespace json {
-inline Parser* Parser::Create(Environment* env, const ParserMethod* method,
- const SymbolTable* symtab,
- Sink* output, bool ignore_json_unknown) {
- return upb_json_parser_create(
- env, method, symtab, output, ignore_json_unknown);
-}
-inline BytesSink* Parser::input() {
- return upb_json_parser_input(this);
-}
-
-inline const BytesHandler* ParserMethod::input_handler() const {
- return upb_json_parsermethod_inputhandler(this);
-}
-/* static */
-inline const ParserMethod* CodeCache::Get(const MessageDef* md) {
- return upb_json_codecache_get(this, md);
-}
+class upb::json::CodeCache {
+ public:
+ CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {}
-} /* namespace json */
-} /* namespace upb */
+ /* Returns a DecoderMethod that can push data to the given handlers.
+ * If a suitable method already exists, it will be returned from the cache. */
+ ParserMethodPtr Get(MessageDefPtr md) {
+ return upb_json_codecache_get(ptr_.get(), md.ptr());
+ }
+
+ private:
+ std::unique_ptr<upb_json_codecache, decltype(&upb_json_codecache_free)> ptr_;
+};
#endif
diff --git a/upb/json/parser.rl b/upb/json/parser.rl
index c2866c9..05a9505 100644
--- a/upb/json/parser.rl
+++ b/upb/json/parser.rl
@@ -299,13 +299,13 @@ static void json_parser_any_frame_set_payload_type(
/* Initialize encoder. */
h = upb_handlercache_get(frame->encoder_handlercache, payload_type);
- encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink);
+ encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink);
/* Initialize parser. */
parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type);
upb_sink_reset(&frame->sink, h, encoder);
frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab,
- &frame->sink, p->ignore_json_unknown);
+ frame->sink, p->ignore_json_unknown);
}
static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) {
@@ -381,9 +381,12 @@ static bool check_stack(upb_json_parser *p) {
static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
upb_value v;
const upb_json_codecache *cache = p->method->cache;
- bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
- const upb_json_parsermethod *method = upb_value_getptr(v);
+ bool ok;
+ const upb_json_parsermethod *method;
+
+ ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
UPB_ASSERT(ok);
+ method = upb_value_getconstptr(v);
frame->name_table = &method->name_table;
}
@@ -1285,7 +1288,7 @@ static bool end_any_stringval(upb_json_parser *p) {
}
json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type);
-
+
return true;
} else {
upb_status_seterrf(
@@ -2661,7 +2664,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_msg_field_iter i;
upb_alloc *alloc = upb_arena_alloc(&c->arena);
- upb_json_parsermethod *m = upb_gmalloc(sizeof(*m));
+ upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
m->cache = c;
@@ -2684,7 +2687,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
size_t len = upb_fielddef_getjsonname(f, NULL, 0);
buf = upb_malloc(alloc, len);
upb_fielddef_getjsonname(f, buf, len);
- upb_strtable_insert3(&m->name_table, buf, len, v, alloc);
+ upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc);
if (strcmp(buf, upb_fielddef_name(f)) != 0) {
/* Since the JSON name is different from the regular field name, add an
@@ -2703,7 +2706,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
upb_json_parser *upb_json_parser_create(upb_env *env,
const upb_json_parsermethod *method,
const upb_symtab* symtab,
- upb_sink *output,
+ upb_sink output,
bool ignore_json_unknown) {
#ifndef NDEBUG
const size_t size_before = upb_env_bytesallocated(env);
@@ -2719,8 +2722,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
upb_bytessink_reset(&p->input_, &method->input_handler_, p);
json_parser_reset(p);
- upb_sink_reset(&p->top->sink, output->handlers, output->closure);
- p->top->m = upb_handlers_msgdef(output->handlers);
+ p->top->sink = output;
+ p->top->m = upb_handlers_msgdef(output.handlers);
if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
p->top->is_any = true;
p->top->any_frame = json_parser_any_frame_new(p);
@@ -2740,8 +2743,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
return p;
}
-upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
- return &p->input_;
+upb_bytessink upb_json_parser_input(upb_json_parser *p) {
+ return p->input_;
}
const upb_byteshandler *upb_json_parsermethod_inputhandler(
@@ -2768,21 +2771,22 @@ void upb_json_codecache_free(upb_json_codecache *c) {
upb_gfree(c);
}
-upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
- const upb_msgdef *md) {
+const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
+ const upb_msgdef *md) {
upb_json_parsermethod *m;
upb_value v;
upb_msg_field_iter i;
+ upb_alloc *alloc = upb_arena_alloc(&c->arena);
if (upb_inttable_lookupptr(&c->methods, md, &v)) {
- return upb_value_getptr(v);
+ return upb_value_getconstptr(v);
}
m = parsermethod_new(c, md);
- v = upb_value_ptr(m);
+ v = upb_value_constptr(m);
if (!m) return NULL;
- if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL;
+ if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL;
/* Populate parser methods for all submessages, so the name tables will
* be available during parsing. */
diff --git a/upb/json/printer.c b/upb/json/printer.c
index b2c9ebd..83f1a58 100644
--- a/upb/json/printer.c
+++ b/upb/json/printer.c
@@ -13,7 +13,7 @@ struct upb_json_printer {
upb_sink input_;
/* BytesSink closure. */
void *subc_;
- upb_bytessink *output_;
+ upb_bytessink output_;
/* We track the depth so that we know when to emit startstr/endstr on the
* output. */
@@ -87,7 +87,7 @@ strpc *newstrpc_str(upb_handlers *h, const char * str) {
static void print_data(
upb_json_printer *p, const char *buf, unsigned int len) {
/* TODO: Will need to change if we support pushback from the sink. */
- size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL);
+ size_t n = upb_bytessink_putbuf(&p->output_, p->subc_, buf, len, NULL);
UPB_ASSERT(n == len);
}
@@ -369,7 +369,7 @@ static bool printer_startmsg(void *closure, const void *handler_data) {
upb_json_printer *p = closure;
UPB_UNUSED(handler_data);
if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
+ upb_bytessink_start(&p->output_, 0, &p->subc_);
}
start_frame(p);
return true;
@@ -381,7 +381,7 @@ static bool printer_endmsg(void *closure, const void *handler_data, upb_status *
UPB_UNUSED(s);
end_frame(p);
if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
+ upb_bytessink_end(&p->output_);
}
return true;
}
@@ -770,7 +770,7 @@ static bool printer_startdurationmsg(void *closure, const void *handler_data) {
upb_json_printer *p = closure;
UPB_UNUSED(handler_data);
if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
+ upb_bytessink_start(&p->output_, 0, &p->subc_);
}
return true;
}
@@ -828,7 +828,7 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data,
print_data(p, "\"", 1);
if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
+ upb_bytessink_end(&p->output_);
}
UPB_UNUSED(handler_data);
@@ -839,7 +839,7 @@ static bool printer_starttimestampmsg(void *closure, const void *handler_data) {
upb_json_printer *p = closure;
UPB_UNUSED(handler_data);
if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
+ upb_bytessink_start(&p->output_, 0, &p->subc_);
}
return true;
}
@@ -902,7 +902,7 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data,
print_data(p, "\"", 1);
if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
+ upb_bytessink_end(&p->output_);
}
UPB_UNUSED(handler_data);
@@ -914,7 +914,7 @@ static bool printer_startmsg_noframe(void *closure, const void *handler_data) {
upb_json_printer *p = closure;
UPB_UNUSED(handler_data);
if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
+ upb_bytessink_start(&p->output_, 0, &p->subc_);
}
return true;
}
@@ -925,7 +925,7 @@ static bool printer_endmsg_noframe(
UPB_UNUSED(handler_data);
UPB_UNUSED(s);
if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
+ upb_bytessink_end(&p->output_);
}
return true;
}
@@ -1253,7 +1253,7 @@ static void json_printer_reset(upb_json_printer *p) {
/* Public API *****************************************************************/
upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
- upb_bytessink *output) {
+ upb_bytessink output) {
#ifndef NDEBUG
size_t size_before = upb_env_bytesallocated(e);
#endif
@@ -1273,12 +1273,16 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
return p;
}
-upb_sink *upb_json_printer_input(upb_json_printer *p) {
- return &p->input_;
+upb_sink upb_json_printer_input(upb_json_printer *p) {
+ return p->input_;
}
upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) {
upb_json_printercache *cache = upb_gmalloc(sizeof(*cache));
+ upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache);
+
cache->preserve_fieldnames = preserve_proto_fieldnames;
- return upb_handlercache_new(printer_sethandlers, cache);
+ upb_handlercache_addcleanup(ret, cache, upb_gfree);
+
+ return ret;
}
diff --git a/upb/json/printer.h b/upb/json/printer.h
index fe9c8f1..a7a37bb 100644
--- a/upb/json/printer.h
+++ b/upb/json/printer.h
@@ -12,44 +12,24 @@
#ifdef __cplusplus
namespace upb {
namespace json {
-class Printer;
+class PrinterPtr;
} /* namespace json */
} /* namespace upb */
#endif
-UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer)
-
-
-/* upb::json::Printer *********************************************************/
+/* upb_json_printer ***********************************************************/
#define UPB_JSON_PRINTER_SIZE 192
-#ifdef __cplusplus
-
-/* Prints an incoming stream of data to a BytesSink in JSON format. */
-class upb::json::Printer {
- public:
- static Printer* Create(Environment* env, const upb::Handlers* handlers,
- BytesSink* output);
-
- /* The input to the printer. */
- Sink* input();
-
- static const size_t kSize = UPB_JSON_PRINTER_SIZE;
- static upb_handlercache* NewCache(bool preserve_proto_fieldnames);
-
- private:
- UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer)
-};
-
-#endif
+struct upb_json_printer;
+typedef struct upb_json_printer upb_json_printer;
UPB_BEGIN_EXTERN_C
/* Native C API. */
upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
- upb_bytessink *output);
-upb_sink *upb_json_printer_input(upb_json_printer *p);
+ upb_bytessink output);
+upb_sink upb_json_printer_input(upb_json_printer *p);
const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
bool preserve_fieldnames,
const void *owner);
@@ -60,19 +40,29 @@ UPB_END_EXTERN_C
#ifdef __cplusplus
-namespace upb {
-namespace json {
-inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
- BytesSink* output) {
- return upb_json_printer_create(env, handlers, output);
-}
-inline Sink* Printer::input() { return upb_json_printer_input(this); }
-inline upb_handlercache* Printer::NewCache(bool preserve_proto_fieldnames) {
- return upb_json_printer_newcache(preserve_proto_fieldnames);
-}
-} /* namespace json */
-} /* namespace upb */
+/* Prints an incoming stream of data to a BytesSink in JSON format. */
+class upb::json::PrinterPtr {
+ public:
+ PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {}
-#endif
+ static PrinterPtr Create(Environment *env, const upb::Handlers *handlers,
+ BytesSink output) {
+ return PrinterPtr(upb_json_printer_create(env, handlers, output.sink()));
+ }
+
+ /* The input to the printer. */
+ Sink input() { return upb_json_printer_input(ptr_); }
+
+ static const size_t kSize = UPB_JSON_PRINTER_SIZE;
+
+ static HandlerCache NewCache(bool preserve_proto_fieldnames) {
+ return upb_json_printer_newcache(preserve_proto_fieldnames);
+ }
+
+ private:
+ upb_json_printer* ptr_;
+};
+
+#endif /* __cplusplus */
#endif /* UPB_JSON_TYPED_PRINTER_H_ */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback