fix crash with js Symbol type

This commit is contained in:
ekibun
2020-08-19 00:43:19 +08:00
parent 0d3aab10ae
commit 7f8938cf7b
4 changed files with 58 additions and 124 deletions

View File

@@ -3,7 +3,7 @@
* @Author: ekibun
* @Date: 2020-08-14 21:45:02
* @LastEditors: ekibun
* @LastEditTime: 2020-08-18 20:29:34
* @LastEditTime: 2020-08-19 00:34:43
*/
#include "../cxx/js_engine.hpp"
#include <flutter_linux/flutter_linux.h>
@@ -13,30 +13,19 @@ namespace std
template <>
struct hash<qjs::Value>
{
std::size_t operator()(const qjs::Value &key) const
size_t operator()(const qjs::Value &key) const
{
return std::hash<std::string>()((std::string)key);
}
};
template <>
struct hash<FlValue>
{
std::size_t operator()(const FlValue *&key) const
{
return 0;
return JS_VALUE_GET_TAG(key.v);
}
};
} // namespace std
namespace qjs
{
JSValue dartToJs(JSContext *ctx, FlValue *val, std::unordered_map<FlValue *, JSValue> cache = std::unordered_map<FlValue *, JSValue>())
JSValue dartToJs(JSContext *ctx, FlValue *val)
{
if (val == nullptr || fl_value_get_type(val) == FL_VALUE_TYPE_NULL)
if (val == nullptr)
return JS_UNDEFINED;
if (cache.find(val) != cache.end())
return cache[val];
FlValueType valType = fl_value_get_type(val);
switch (valType)
{
@@ -59,7 +48,6 @@ namespace qjs
auto buf = fl_value_get_float_list(val);
auto size = (uint32_t)fl_value_get_length(val);
JSValue array = JS_NewArray(ctx);
cache[val] = array;
for (uint32_t i = 0; i < size; ++i)
JS_DefinePropertyValue(
ctx, array, JS_NewAtomUInt32(ctx, i), JS_NewFloat64(ctx, buf[i]),
@@ -70,11 +58,10 @@ namespace qjs
{
auto size = (uint32_t)fl_value_get_length(val);
JSValue array = JS_NewArray(ctx);
cache[val] = array;
for (uint32_t i = 0; i < size; ++i)
JS_DefinePropertyValue(
ctx, array, JS_NewAtomUInt32(ctx, i),
dartToJs(ctx, fl_value_get_list_value(val, i), cache),
dartToJs(ctx, fl_value_get_list_value(val, i)),
JS_PROP_C_W_E);
return array;
}
@@ -82,46 +69,39 @@ namespace qjs
{
auto size = (uint32_t)fl_value_get_length(val);
JSValue obj = JS_NewObject(ctx);
cache[val] = obj;
for (uint32_t i = 0; i < size; ++i)
JS_DefinePropertyValue(
ctx, obj,
JS_ValueToAtom(ctx, dartToJs(ctx, fl_value_get_map_key(val, i), cache)),
dartToJs(ctx, fl_value_get_map_value(val, i), cache),
JS_ValueToAtom(ctx, dartToJs(ctx, fl_value_get_map_key(val, i))),
dartToJs(ctx, fl_value_get_map_value(val, i)),
JS_PROP_C_W_E);
return obj;
}
default:
return JS_UNDEFINED;
}
return JS_UNDEFINED;
}
FlValue *jsToDart(Value val, std::unordered_map<Value, FlValue *> cache = std::unordered_map<Value, FlValue *>())
{
if (JS_IsUndefined(val.v) || JS_IsNull(val.v) || JS_IsUninitialized(val.v))
return fl_value_new_null();
if (cache.find(val) != cache.end())
return cache[val];
if (JS_IsBool(val.v))
int tag = JS_VALUE_GET_TAG(val.v);
if (JS_TAG_IS_FLOAT64(tag))
return fl_value_new_float((double)val);
switch (tag)
{
case JS_TAG_BOOL:
return fl_value_new_bool((bool)val);
{ // Number
int tag = JS_VALUE_GET_TAG(val.v);
if (tag == JS_TAG_INT)
return fl_value_new_int((int64_t)val);
else if (JS_TAG_IS_FLOAT64(tag))
return fl_value_new_float((double)val);
}
if (JS_IsString(val.v))
case JS_TAG_INT:
return fl_value_new_int((int64_t)val);
case JS_TAG_STRING:
return fl_value_new_string(((std::string)val).c_str());
case JS_TAG_OBJECT:
{ // ArrayBuffer
size_t size;
uint8_t *buf = JS_GetArrayBuffer(val.ctx, &size, val.v);
if (buf)
return fl_value_new_uint8_list(buf, size);
}
if (JS_IsObject(val.v))
{
if (JS_IsFunction(val.ctx, val.v))
{
FlValue *retMap = fl_value_new_map();
@@ -158,7 +138,8 @@ namespace qjs
js_free(val.ctx, ptab);
return retMap;
}
default:
return fl_value_new_null();
}
return fl_value_new_null();
}
} // namespace qjs