2019-06-22 21:20:28 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-23 05:21:30 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
// TODO replace with comptime code generation.
|
|
|
|
// TODO remove cJSON dependency.
|
|
|
|
// OLD: User decode_User(string js) {
|
|
|
|
// now it's
|
|
|
|
// User decode_User(cJSON* root) {
|
|
|
|
// User res;
|
|
|
|
// res.name = decode_string(js_get(root, "name"));
|
|
|
|
// res.profile = decode_Profile(js_get(root, "profile"));
|
|
|
|
// return res;
|
|
|
|
// }
|
|
|
|
// Codegen json_decode/encode funcs
|
|
|
|
fn (p mut Parser) gen_json_for_type(typ Type) {
|
|
|
|
mut dec := ''
|
|
|
|
mut enc := ''
|
|
|
|
t := typ.name
|
|
|
|
if t == 'int' || t == 'string' || t == 'bool' {
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if p.first_pass() {
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// println('gen_json_for_type( $typ.name )')
|
|
|
|
// Register decoder fn
|
|
|
|
mut dec_fn := Fn {
|
2019-08-05 10:49:52 +03:00
|
|
|
mod: p.mod
|
2019-06-22 21:20:28 +03:00
|
|
|
typ: 'Option_$typ.name'
|
|
|
|
name: js_dec_name(t)
|
|
|
|
}
|
|
|
|
// Already registered? Skip.
|
|
|
|
if p.table.known_fn(dec_fn.name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// decode_TYPE funcs receive an actual cJSON* object to decode
|
|
|
|
// cJSON_Parse(str) call is added by the compiler
|
|
|
|
arg := Var {
|
|
|
|
typ: 'cJSON*'
|
|
|
|
}
|
|
|
|
dec_fn.args << arg
|
|
|
|
p.table.register_fn(dec_fn)
|
|
|
|
// Register encoder fn
|
|
|
|
mut enc_fn := Fn {
|
2019-08-05 10:49:52 +03:00
|
|
|
mod: p.mod
|
2019-06-22 21:20:28 +03:00
|
|
|
typ: 'cJSON*'
|
|
|
|
name: js_enc_name(t)
|
|
|
|
}
|
|
|
|
// encode_TYPE funcs receive an object to encode
|
|
|
|
enc_arg := Var {
|
|
|
|
typ: t
|
|
|
|
}
|
|
|
|
enc_fn.args << enc_arg
|
|
|
|
p.table.register_fn(enc_fn)
|
|
|
|
// Code gen decoder
|
|
|
|
dec += '
|
2019-12-08 22:22:47 +03:00
|
|
|
//$t $dec_fn.name (cJSON* root) {
|
|
|
|
Option ${dec_fn.name}(cJSON* root, $t* res) {
|
2019-08-17 22:19:37 +03:00
|
|
|
// $t res;
|
2019-06-22 21:20:28 +03:00
|
|
|
if (!root) {
|
|
|
|
const char *error_ptr = cJSON_GetErrorPtr();
|
|
|
|
if (error_ptr != NULL) {
|
|
|
|
fprintf(stderr, "Error in decode() for $t error_ptr=: %%s\\n", error_ptr);
|
2019-08-17 22:19:37 +03:00
|
|
|
// printf("\\nbad js=%%s\\n", js.str);
|
2019-06-23 01:47:49 +03:00
|
|
|
return v_error(tos2(error_ptr));
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
// Code gen encoder
|
|
|
|
enc += '
|
2019-12-08 22:22:47 +03:00
|
|
|
cJSON* $enc_fn.name ($t val) {
|
2019-06-22 21:20:28 +03:00
|
|
|
cJSON *o = cJSON_CreateObject();
|
2019-08-17 22:19:37 +03:00
|
|
|
string res = tos2("");
|
2019-06-22 21:20:28 +03:00
|
|
|
'
|
|
|
|
// Handle arrays
|
|
|
|
if t.starts_with('array_') {
|
|
|
|
dec += p.decode_array(t)
|
|
|
|
enc += p.encode_array(t)
|
|
|
|
}
|
|
|
|
// Range through fields
|
|
|
|
for field in typ.fields {
|
|
|
|
if field.attr == 'skip' {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-23 02:54:39 +03:00
|
|
|
name := if field.attr.starts_with('json:') {
|
2019-10-27 10:03:15 +03:00
|
|
|
field.attr[5..]
|
2019-08-23 02:54:39 +03:00
|
|
|
} else {
|
|
|
|
field.name
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
field_type := p.table.find_type(field.typ)
|
|
|
|
_typ := field.typ.replace('*', '')
|
|
|
|
enc_name := js_enc_name(_typ)
|
2019-08-14 17:38:28 +03:00
|
|
|
if field.attr == 'raw' {
|
2019-08-23 02:54:39 +03:00
|
|
|
dec += ' res->$field.name = tos2(cJSON_PrintUnformatted(' +
|
|
|
|
'js_get(root, "$name")));\n'
|
2019-12-08 22:22:47 +03:00
|
|
|
|
2019-08-14 17:38:28 +03:00
|
|
|
} else {
|
|
|
|
// Now generate decoders for all field types in this struct
|
|
|
|
// need to do it here so that these functions are generated first
|
|
|
|
p.gen_json_for_type(field_type)
|
2019-12-08 22:22:47 +03:00
|
|
|
|
2019-08-14 17:38:28 +03:00
|
|
|
dec_name := js_dec_name(_typ)
|
|
|
|
|
|
|
|
if is_js_prim(_typ) {
|
2019-12-08 22:22:47 +03:00
|
|
|
dec += ' res->$field.name = $dec_name (js_get(' +
|
2019-08-23 02:54:39 +03:00
|
|
|
'root, "$name"))'
|
2019-08-14 17:38:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-08 22:22:47 +03:00
|
|
|
dec += ' $dec_name (js_get(root, "$name"), & (res->$field.name))'
|
2019-08-14 17:38:28 +03:00
|
|
|
}
|
|
|
|
dec += ';\n'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-12-08 22:22:47 +03:00
|
|
|
enc += ' cJSON_AddItemToObject(o, "$name",$enc_name (val.$field.name)); \n'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// cJSON_delete
|
2019-07-03 22:07:42 +03:00
|
|
|
//p.cgen.fns << '$dec return opt_ok(res); \n}'
|
2019-07-03 22:47:49 +03:00
|
|
|
p.cgen.fns << '$dec return opt_ok(res, sizeof(*res)); \n}'
|
2019-06-22 21:20:28 +03:00
|
|
|
p.cgen.fns << '/*enc start*/ $enc return o;}'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_js_prim(typ string) bool {
|
2019-06-24 23:25:35 +03:00
|
|
|
return typ == 'int' || typ == 'string' ||
|
2019-06-25 22:36:44 +03:00
|
|
|
typ == 'bool' || typ == 'f32' || typ == 'f64' ||
|
2019-09-01 22:37:22 +03:00
|
|
|
typ == 'i8' || typ == 'i16' || typ == 'i64' ||
|
|
|
|
typ == 'u16' || typ == 'u32' || typ == 'u64'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn (p mut Parser) decode_array(array_type string) string {
|
|
|
|
typ := array_type.replace('array_', '')
|
2019-06-22 21:20:28 +03:00
|
|
|
t := p.table.find_type(typ)
|
|
|
|
fn_name := js_dec_name(typ)
|
|
|
|
// If we have `[]Profile`, have to register a Profile en(de)coder first
|
|
|
|
p.gen_json_for_type(t)
|
|
|
|
mut s := ''
|
|
|
|
if is_js_prim(typ) {
|
2019-12-08 22:22:47 +03:00
|
|
|
s = '$typ val= $fn_name (jsval); '
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-08 22:22:47 +03:00
|
|
|
s = ' $typ val; $fn_name (jsval, &val); '
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return '
|
|
|
|
*res = new_array(0, 0, sizeof($typ));
|
|
|
|
const cJSON *jsval = NULL;
|
|
|
|
cJSON_ArrayForEach(jsval, root)
|
|
|
|
{
|
2019-08-17 22:19:37 +03:00
|
|
|
$s
|
2019-10-10 01:07:00 +03:00
|
|
|
array_push(res, &val);
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn js_enc_name(typ string) string {
|
|
|
|
name := 'json__jsencode_$typ'
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn js_dec_name(typ string) string {
|
|
|
|
name := 'json__jsdecode_$typ'
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn (p &Parser) encode_array(array_type string) string {
|
|
|
|
typ := array_type.replace('array_', '')
|
2019-06-22 21:20:28 +03:00
|
|
|
fn_name := js_enc_name(typ)
|
|
|
|
return '
|
|
|
|
o = cJSON_CreateArray();
|
|
|
|
for (int i = 0; i < val.len; i++){
|
2019-12-08 22:22:47 +03:00
|
|
|
cJSON_AddItemToArray(o, $fn_name ( (($typ*)val.data)[i] ));
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
'
|
|
|
|
}
|
|
|
|
|