mirror of
https://github.com/pocketpy/pocketpy
synced 2026-08-06 22:27:11 +08:00
improve mpack
This commit is contained in:
parent
4816c4471b
commit
4fc4d665ec
@ -251,7 +251,7 @@
|
|||||||
* types in @ref docs/protocol.md for more information.
|
* types in @ref docs/protocol.md for more information.
|
||||||
*/
|
*/
|
||||||
#ifndef MPACK_EXTENSIONS
|
#ifndef MPACK_EXTENSIONS
|
||||||
#define MPACK_EXTENSIONS 0
|
#define MPACK_EXTENSIONS 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
12
3rd/msgpack/include/mpack1.h
Normal file
12
3rd/msgpack/include/mpack1.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "mpack.h"
|
||||||
|
#include "pocketpy.h"
|
||||||
|
|
||||||
|
typedef bool (*is_mpack_ext_t)(py_Type type);
|
||||||
|
typedef bool (*py_to_mpack_ext_t)(py_Ref object, mpack_writer_t* writer);
|
||||||
|
typedef bool (*mpack_to_py_ext_t)(mpack_node_t node);
|
||||||
|
|
||||||
|
void mpack_config_exttype_callbacks(is_mpack_ext_t is_ext,
|
||||||
|
py_to_mpack_ext_t to_mpack,
|
||||||
|
mpack_to_py_ext_t to_py);
|
||||||
@ -1,8 +1,22 @@
|
|||||||
#include "pocketpy.h"
|
#include "pocketpy.h"
|
||||||
|
|
||||||
#include "mpack.h"
|
#include "mpack1.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
_Static_assert(MPACK_EXTENSIONS == 1, "You should change MPACK_EXTENSIONS in mpack.h to 1");
|
||||||
|
|
||||||
|
static is_mpack_ext_t is_mpack_ext;
|
||||||
|
static py_to_mpack_ext_t py_to_mpack_ext;
|
||||||
|
static mpack_to_py_ext_t mpack_to_py_ext;
|
||||||
|
|
||||||
|
void mpack_config_exttype_callbacks(is_mpack_ext_t is_ext,
|
||||||
|
py_to_mpack_ext_t to_mpack,
|
||||||
|
mpack_to_py_ext_t to_py) {
|
||||||
|
is_mpack_ext = is_ext;
|
||||||
|
py_to_mpack_ext = to_mpack;
|
||||||
|
mpack_to_py_ext = to_py;
|
||||||
|
}
|
||||||
|
|
||||||
static bool mpack_to_py(mpack_node_t node) {
|
static bool mpack_to_py(mpack_node_t node) {
|
||||||
py_StackRef tmp = py_pushtmp();
|
py_StackRef tmp = py_pushtmp();
|
||||||
|
|
||||||
@ -69,6 +83,14 @@ static bool mpack_to_py(mpack_node_t node) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case mpack_type_ext: {
|
||||||
|
if(!mpack_to_py_ext) {
|
||||||
|
return RuntimeError("msgpack: mpack_to_py_ext is not set, cannot handle ext type");
|
||||||
|
}
|
||||||
|
if(!mpack_to_py_ext(node)) return false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: return ValueError("msgpack: invalid node type");
|
default: return ValueError("msgpack: invalid node type");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -153,6 +175,11 @@ static bool py_to_mpack(py_Ref object, mpack_writer_t* writer) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
if(is_mpack_ext && is_mpack_ext(object->type)) {
|
||||||
|
assert(py_to_mpack_ext != NULL);
|
||||||
|
return py_to_mpack_ext(object, writer);
|
||||||
|
break;
|
||||||
|
}
|
||||||
mpack_write_nil(writer);
|
mpack_write_nil(writer);
|
||||||
return TypeError("msgpack: unsupported type '%t'", object->type);
|
return TypeError("msgpack: unsupported type '%t'", object->type);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user