mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Add BigInt to se::Value (#3706)
This commit is contained in:
@@ -55,9 +55,9 @@ static bool jsb_BufferPool_constructor(se::State &s) { // NOLINT
|
||||
uint bytesPerEntry = 0;
|
||||
|
||||
bool ok = true;
|
||||
ok &= seval_to_uint(args[0], &poolType);
|
||||
ok &= seval_to_uint(args[1], &entryBits);
|
||||
ok &= seval_to_uint(args[2], &bytesPerEntry);
|
||||
ok &= seval_to_uint32(args[0], &poolType);
|
||||
ok &= seval_to_uint32(args[1], &entryBits);
|
||||
ok &= seval_to_uint32(args[2], &bytesPerEntry);
|
||||
if (!ok) {
|
||||
SE_REPORT_ERROR("jsb_BufferPool_constructor: argument convertion error");
|
||||
return false;
|
||||
@@ -141,9 +141,9 @@ static bool jsb_BufferAllocator_alloc(se::State &s) { // NOLINT
|
||||
size_t argc = args.size();
|
||||
if (argc == 2) {
|
||||
uint index = 0;
|
||||
seval_to_uint(args[0], &index);
|
||||
seval_to_uint32(args[0], &index);
|
||||
uint bytes = 0;
|
||||
seval_to_uint(args[1], &bytes);
|
||||
seval_to_uint32(args[1], &bytes);
|
||||
s.rval().setObject(bufferAllocator->alloc(index, bytes));
|
||||
return true;
|
||||
}
|
||||
@@ -161,7 +161,7 @@ static bool jsb_BufferAllocator_free(se::State &s) { // NOLINT
|
||||
size_t argc = args.size();
|
||||
if (argc == 1) {
|
||||
uint index = 0;
|
||||
seval_to_uint(args[0], &index);
|
||||
seval_to_uint32(args[0], &index);
|
||||
bufferAllocator->free(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -25,13 +25,14 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "Value.h"
|
||||
#include <sstream>
|
||||
#include "Object.h"
|
||||
|
||||
namespace se {
|
||||
|
||||
ValueArray EmptyValueArray;
|
||||
ValueArray EmptyValueArray; // NOLINT(readability-identifier-naming)
|
||||
|
||||
Value Value::Null = Value(Type::Null);
|
||||
Value Value::Null = Value(Type::Null);
|
||||
Value Value::Undefined = Value(Type::Undefined);
|
||||
|
||||
Value::Value()
|
||||
@@ -53,7 +54,7 @@ Value::Value(const Value &v)
|
||||
*this = v;
|
||||
}
|
||||
|
||||
Value::Value(Value &&v)
|
||||
Value::Value(Value &&v) noexcept
|
||||
: _type(Type::Undefined),
|
||||
_autoRootUnroot(false) {
|
||||
*this = std::move(v);
|
||||
@@ -101,16 +102,16 @@ Value::Value(uint16_t v)
|
||||
setUint16(v);
|
||||
}
|
||||
|
||||
Value::Value(long v)
|
||||
Value::Value(int64_t v)
|
||||
: _type(Type::Undefined),
|
||||
_autoRootUnroot(false) {
|
||||
setLong(v);
|
||||
setInt64(v);
|
||||
}
|
||||
|
||||
Value::Value(unsigned long v)
|
||||
Value::Value(uint64_t v)
|
||||
: _type(Type::Undefined),
|
||||
_autoRootUnroot(false) {
|
||||
setUlong(v);
|
||||
setUint64(v);
|
||||
}
|
||||
|
||||
Value::Value(float v)
|
||||
@@ -122,7 +123,7 @@ Value::Value(float v)
|
||||
Value::Value(double v)
|
||||
: _type(Type::Undefined),
|
||||
_autoRootUnroot(false) {
|
||||
setNumber(v);
|
||||
setDouble(v);
|
||||
}
|
||||
|
||||
Value::Value(const char *v)
|
||||
@@ -166,6 +167,9 @@ Value &Value::operator=(const Value &v) {
|
||||
case Type::Number:
|
||||
_u._number = v._u._number;
|
||||
break;
|
||||
case Type::BigInt:
|
||||
_u._bigint = v._u._bigint;
|
||||
break;
|
||||
case Type::String:
|
||||
*_u._string = *v._u._string;
|
||||
break;
|
||||
@@ -182,7 +186,7 @@ Value &Value::operator=(const Value &v) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Value &Value::operator=(Value &&v) {
|
||||
Value &Value::operator=(Value &&v) noexcept {
|
||||
if (this != &v) {
|
||||
reset(v.getType());
|
||||
|
||||
@@ -195,6 +199,9 @@ Value &Value::operator=(Value &&v) {
|
||||
case Type::Number:
|
||||
_u._number = v._u._number;
|
||||
break;
|
||||
case Type::BigInt:
|
||||
_u._bigint = v._u._bigint;
|
||||
break;
|
||||
case Type::String:
|
||||
*_u._string = std::move(*v._u._string);
|
||||
break;
|
||||
@@ -209,9 +216,9 @@ Value &Value::operator=(Value &&v) {
|
||||
}
|
||||
_u._object->decRef();
|
||||
}
|
||||
_u._object = v._u._object;
|
||||
_autoRootUnroot = v._autoRootUnroot;
|
||||
v._u._object = nullptr; // Reset to nullptr here to avoid 'release' operation in v.reset(Type::Undefined) since it's a move operation here.
|
||||
_u._object = v._u._object;
|
||||
_autoRootUnroot = v._autoRootUnroot;
|
||||
v._u._object = nullptr; // Reset to nullptr here to avoid 'release' operation in v.reset(Type::Undefined) since it's a move operation here.
|
||||
v._autoRootUnroot = false;
|
||||
} break;
|
||||
default:
|
||||
@@ -268,55 +275,50 @@ void Value::setBoolean(bool v) {
|
||||
|
||||
void Value::setInt8(int8_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setUint8(uint8_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setInt32(int32_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setUint32(uint32_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setInt16(int16_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setUint16(uint16_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setLong(long v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
void Value::setInt64(int64_t v) {
|
||||
reset(Type::BigInt);
|
||||
_u._bigint = v;
|
||||
}
|
||||
|
||||
void Value::setUIntptr_t(uintptr_t v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
}
|
||||
|
||||
void Value::setUlong(unsigned long v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
void Value::setUint64(uint64_t v) {
|
||||
reset(Type::BigInt);
|
||||
_u._bigint = static_cast<int64_t>(v);
|
||||
}
|
||||
|
||||
void Value::setFloat(float v) {
|
||||
reset(Type::Number);
|
||||
_u._number = (double)v;
|
||||
_u._number = static_cast<double>(v);
|
||||
}
|
||||
|
||||
void Value::setNumber(double v) {
|
||||
void Value::setDouble(double v) {
|
||||
reset(Type::Number);
|
||||
_u._number = v;
|
||||
}
|
||||
@@ -360,7 +362,7 @@ void Value::setObject(Object *object, bool autoRootUnroot /* = false*/) {
|
||||
}
|
||||
_u._object->decRef();
|
||||
}
|
||||
_u._object = object;
|
||||
_u._object = object;
|
||||
_autoRootUnroot = autoRootUnroot;
|
||||
} else {
|
||||
_autoRootUnroot = autoRootUnroot;
|
||||
@@ -375,56 +377,51 @@ void Value::setObject(const HandleObject &o, bool autoRootUnroot /* = false*/) {
|
||||
}
|
||||
|
||||
int8_t Value::toInt8() const {
|
||||
return static_cast<int8_t>(toNumber());
|
||||
return static_cast<int8_t>(toDouble());
|
||||
}
|
||||
|
||||
uint8_t Value::toUint8() const {
|
||||
return static_cast<uint8_t>(toNumber());
|
||||
return static_cast<uint8_t>(toDouble());
|
||||
}
|
||||
|
||||
int16_t Value::toInt16() const {
|
||||
return static_cast<int16_t>(toNumber());
|
||||
return static_cast<int16_t>(toDouble());
|
||||
}
|
||||
|
||||
uint16_t Value::toUint16() const {
|
||||
return static_cast<uint16_t>(toNumber());
|
||||
return static_cast<uint16_t>(toDouble());
|
||||
}
|
||||
|
||||
int32_t Value::toInt32() const {
|
||||
return static_cast<int32_t>(toNumber());
|
||||
return static_cast<int32_t>(toDouble());
|
||||
}
|
||||
|
||||
uint32_t Value::toUint32() const {
|
||||
return static_cast<uint32_t>(toNumber());
|
||||
return static_cast<uint32_t>(toDouble());
|
||||
}
|
||||
|
||||
unsigned int Value::toUint() const {
|
||||
return static_cast<unsigned int>(toNumber());
|
||||
int64_t Value::toInt64() const {
|
||||
assert(isBigInt() || isNumber());
|
||||
return _type == Type::BigInt ? _u._bigint : static_cast<uint64_t>(_u._number);
|
||||
}
|
||||
|
||||
long Value::toLong() const {
|
||||
return static_cast<long>(toNumber());
|
||||
}
|
||||
|
||||
unsigned long Value::toUlong() const {
|
||||
return static_cast<unsigned long>(toNumber());
|
||||
}
|
||||
|
||||
intptr_t Value::toUIntptr_t() const {
|
||||
return static_cast<uintptr_t>(toNumber());
|
||||
uint64_t Value::toUint64() const {
|
||||
assert(isBigInt() || isNumber());
|
||||
return _type == Type::BigInt ? static_cast<uint64_t>(_u._bigint) : static_cast<uint64_t>(_u._number);
|
||||
}
|
||||
|
||||
float Value::toFloat() const {
|
||||
return static_cast<float>(toNumber());
|
||||
return static_cast<float>(toDouble());
|
||||
}
|
||||
|
||||
double Value::toNumber() const {
|
||||
double Value::toDouble() const {
|
||||
assert(_type == Type::Number || _type == Type::Boolean);
|
||||
// assert(_type != Type::BigInt);
|
||||
if (_type == Type::Boolean) {
|
||||
if (_u._boolean)
|
||||
if (_u._boolean) {
|
||||
return 1.0;
|
||||
else
|
||||
return 0.0;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
return _u._number;
|
||||
}
|
||||
@@ -440,26 +437,28 @@ const std::string &Value::toString() const {
|
||||
}
|
||||
|
||||
std::string Value::toStringForce() const {
|
||||
std::string ret;
|
||||
std::stringstream ss;
|
||||
if (_type == Type::String) {
|
||||
ret = *_u._string;
|
||||
ss << *_u._string;
|
||||
} else if (_type == Type::Boolean) {
|
||||
ret = _u._boolean ? "true" : "false";
|
||||
ss << (_u._boolean ? "true" : "false");
|
||||
} else if (_type == Type::Number) {
|
||||
char tmp[50] = {0};
|
||||
snprintf(tmp, sizeof(tmp), "%.17g", _u._number);
|
||||
ret = tmp;
|
||||
ss << tmp;
|
||||
} else if (_type == Type::BigInt) {
|
||||
ss << _u._bigint;
|
||||
} else if (_type == Type::Object) {
|
||||
ret = toObject()->toString();
|
||||
ss << toObject()->toString();
|
||||
} else if (_type == Type::Null) {
|
||||
ret = "null";
|
||||
ss << "null";
|
||||
} else if (_type == Type::Undefined) {
|
||||
ret = "undefined";
|
||||
ss << "undefined";
|
||||
} else {
|
||||
assert(false);
|
||||
ss << "[[BadValueType]]";
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Object *Value::toObject() const {
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "HandleObject.h"
|
||||
|
||||
@@ -42,16 +42,17 @@ class Object;
|
||||
class Value final {
|
||||
public:
|
||||
enum class Type : char {
|
||||
Undefined = 0,
|
||||
Null,
|
||||
Number,
|
||||
Boolean,
|
||||
String,
|
||||
Object
|
||||
Undefined = 0, // NOLINT(readability-identifier-naming)
|
||||
Null, // NOLINT(readability-identifier-naming)
|
||||
Number, // NOLINT(readability-identifier-naming)
|
||||
Boolean, // NOLINT(readability-identifier-naming)
|
||||
String, // NOLINT(readability-identifier-naming)
|
||||
Object, // NOLINT(readability-identifier-naming)
|
||||
BigInt, // NOLINT(readability-identifier-naming)
|
||||
};
|
||||
|
||||
static Value Null;
|
||||
static Value Undefined;
|
||||
static Value Null; // NOLINT(readability-identifier-naming)
|
||||
static Value Undefined; // NOLINT(readability-identifier-naming)
|
||||
|
||||
/**
|
||||
* @brief The default constructor.
|
||||
@@ -66,60 +67,60 @@ public:
|
||||
/**
|
||||
* @brief The move constructor.
|
||||
*/
|
||||
Value(Value &&v);
|
||||
Value(Value &&v) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The constructor with a boolean arguement.
|
||||
* @brief The constructor with a boolean argument.
|
||||
*/
|
||||
explicit Value(bool v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a int8_t arguement.
|
||||
* @brief The constructor with a int8_t argument.
|
||||
*/
|
||||
explicit Value(int8_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a uint8_t arguement.
|
||||
* @brief The constructor with a uint8_t argument.
|
||||
*/
|
||||
explicit Value(uint8_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a int16_t arguement.
|
||||
* @brief The constructor with a int16_t argument.
|
||||
*/
|
||||
explicit Value(int16_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a uint16_t arguement.
|
||||
* @brief The constructor with a uint16_t argument.
|
||||
*/
|
||||
explicit Value(uint16_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a int32_t arguement.
|
||||
* @brief The constructor with a int32_t argument.
|
||||
*/
|
||||
explicit Value(int32_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a uint32_t arguement.
|
||||
* @brief The constructor with a uint32_t argument.
|
||||
*/
|
||||
explicit Value(uint32_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a long arguement.
|
||||
* @brief The constructor with a uint64_t argument.
|
||||
*/
|
||||
explicit Value(long v);
|
||||
explicit Value(uint64_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a unsigned long arguement.
|
||||
* @brief The constructor with a int64_t argument.
|
||||
*/
|
||||
explicit Value(unsigned long v);
|
||||
explicit Value(int64_t v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a float arguement.
|
||||
* @brief The constructor with a float argument.
|
||||
*/
|
||||
explicit Value(float v);
|
||||
|
||||
/**
|
||||
* @brief The constructor with a double arguement.
|
||||
* @brief The constructor with a double argument.
|
||||
*/
|
||||
explicit Value(double v);
|
||||
|
||||
@@ -160,7 +161,7 @@ public:
|
||||
/**
|
||||
* @brief The move assignment operator.
|
||||
*/
|
||||
Value &operator=(Value &&v);
|
||||
Value &operator=(Value &&v) noexcept;
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to undefined.
|
||||
@@ -215,34 +216,28 @@ public:
|
||||
void setUint32(uint32_t v);
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to a long value.
|
||||
* @param[in] v The long value to be set.
|
||||
*/
|
||||
void setLong(long v);
|
||||
* @brief Sets se::Value to a unsigned int64_t
|
||||
* @param[in] v The unsigned int64_t value to be set.
|
||||
*/
|
||||
void setUint64(uint64_t v);
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to a uintptr_t value.
|
||||
* @param[in] v The uintptr_t value to be set.
|
||||
*/
|
||||
void setUIntptr_t(uintptr_t v);
|
||||
* @brief Sets se::Value to a int64_t value.
|
||||
* @param[in] v The int64_t value to be set.
|
||||
*/
|
||||
void setInt64(int64_t v);
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to a unsigned long value.
|
||||
* @param[in] v The unsigned long value to be set.
|
||||
*/
|
||||
void setUlong(unsigned long v);
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to a float value.
|
||||
* @param[in] v The float value to be set.
|
||||
*/
|
||||
* @brief Sets se::Value to a float value.
|
||||
* @param[in] v The float value to be set.
|
||||
*/
|
||||
void setFloat(float v);
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to a double value.
|
||||
* @param[in] v The double value to be set.
|
||||
*/
|
||||
void setNumber(double v);
|
||||
void setDouble(double v);
|
||||
|
||||
/**
|
||||
* @brief Sets se::Value to an UTF8 null-terminated string value.
|
||||
@@ -306,25 +301,18 @@ public:
|
||||
*/
|
||||
uint32_t toUint32() const;
|
||||
|
||||
unsigned int toUint() const;
|
||||
/**
|
||||
* @brief Converts se::Value to int64_t
|
||||
* @return signed int64
|
||||
*/
|
||||
int64_t toInt64() const;
|
||||
|
||||
/**
|
||||
* @brief Converts se::Value to long.
|
||||
* @return long integer.
|
||||
*/
|
||||
long toLong() const;
|
||||
* @brief Converts se::Value to unsigned uint64_t.
|
||||
* @return unsigned int64.
|
||||
*/
|
||||
uint64_t toUint64() const;
|
||||
|
||||
/**
|
||||
* @brief Converts se::Value to unsigned long.
|
||||
* @return unsigned long integer.
|
||||
*/
|
||||
unsigned long toUlong() const;
|
||||
|
||||
/**
|
||||
* @brief Converts se::Value to unsigned long.
|
||||
* @return intptr_t.
|
||||
*/
|
||||
intptr_t toUIntptr_t() const;
|
||||
/**
|
||||
* @brief Converts se::Value to float number.
|
||||
* @return float number.
|
||||
@@ -332,10 +320,10 @@ public:
|
||||
float toFloat() const;
|
||||
|
||||
/**
|
||||
* @brief Converts se::Value to double number.
|
||||
* @return double number.
|
||||
*/
|
||||
double toNumber() const;
|
||||
* @brief Converts se::Value to double number.
|
||||
* @return double number.
|
||||
*/
|
||||
double toDouble() const;
|
||||
|
||||
/**
|
||||
* @brief Converts se::Value to boolean.
|
||||
@@ -370,15 +358,21 @@ public:
|
||||
inline Type getType() const { return _type; }
|
||||
|
||||
/**
|
||||
* @brief Tests whether se::Value stores a number.
|
||||
* @return true if se::Value stores a number, otherwise false.
|
||||
*/
|
||||
* @brief Tests whether se::Value stores a number.
|
||||
* @return true if se::Value stores a number, otherwise false.
|
||||
*/
|
||||
inline bool isNumber() const { return _type == Type::Number; }
|
||||
|
||||
/**
|
||||
* @brief Tests whether se::Value stores a string.
|
||||
* @return true if se::Value stores a string, otherwise false.
|
||||
*/
|
||||
* @brief Tests whether se::Value stores a Bigint.
|
||||
* @return true if se::Value stores a uint64_t or a int64_t, otherwise false.
|
||||
*/
|
||||
inline bool isBigInt() const { return _type == Type::BigInt; }
|
||||
|
||||
/**
|
||||
* @brief Tests whether se::Value stores a string.
|
||||
* @return true if se::Value stores a string, otherwise false.
|
||||
*/
|
||||
inline bool isString() const { return _type == Type::String; }
|
||||
|
||||
/**
|
||||
@@ -411,15 +405,28 @@ public:
|
||||
*/
|
||||
inline bool isNullOrUndefined() const { return (isNull() || isUndefined()); }
|
||||
|
||||
size_t toSize() const {
|
||||
return static_cast<size_t>(toDouble());
|
||||
}
|
||||
|
||||
void setSize(size_t v) {
|
||||
setDouble(static_cast<double>(v));
|
||||
}
|
||||
|
||||
uintptr_t asPtr() const {
|
||||
return static_cast<uintptr_t>(toUint64());
|
||||
}
|
||||
|
||||
private:
|
||||
explicit Value(Type type);
|
||||
void reset(Type type);
|
||||
|
||||
union {
|
||||
bool _boolean;
|
||||
double _number;
|
||||
bool _boolean;
|
||||
double _number;
|
||||
std::string *_string;
|
||||
Object *_object;
|
||||
Object * _object;
|
||||
int64_t _bigint;
|
||||
} _u;
|
||||
|
||||
Type _type;
|
||||
@@ -427,8 +434,8 @@ private:
|
||||
};
|
||||
|
||||
using ValueArray = std::vector<Value>;
|
||||
extern ValueArray EmptyValueArray;
|
||||
extern ValueArray EmptyValueArray; // NOLINT(readability-identifier-naming)
|
||||
|
||||
} // namespace se
|
||||
|
||||
typedef se::Object *se_object_ptr;
|
||||
using se_object_ptr = se::Object *; // NOLINT(readability-identifier-naming)
|
||||
|
||||
@@ -59,7 +59,7 @@ void seToJsValue(v8::Isolate *isolate, const Value &v, v8::Local<v8::Value> *out
|
||||
assert(outJsVal != nullptr);
|
||||
switch (v.getType()) {
|
||||
case Value::Type::Number:
|
||||
*outJsVal = v8::Number::New(isolate, v.toNumber());
|
||||
*outJsVal = v8::Number::New(isolate, v.toDouble());
|
||||
break;
|
||||
case Value::Type::String: {
|
||||
v8::MaybeLocal<v8::String> str = v8::String::NewFromUtf8(isolate, v.toString().data(), v8::NewStringType::kNormal, static_cast<int>(v.toString().length()));
|
||||
@@ -81,7 +81,9 @@ void seToJsValue(v8::Isolate *isolate, const Value &v, v8::Local<v8::Value> *out
|
||||
case Value::Type::Undefined:
|
||||
*outJsVal = v8::Undefined(isolate);
|
||||
break;
|
||||
|
||||
case Value::Type::BigInt:
|
||||
*outJsVal = v8::BigInt::New(isolate, v.toInt64());
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
@@ -99,7 +101,15 @@ void jsToSeValue(v8::Isolate *isolate, v8::Local<v8::Value> jsval, Value *v) {
|
||||
} else if (jsval->IsNumber()) {
|
||||
v8::MaybeLocal<v8::Number> jsNumber = jsval->ToNumber(isolate->GetCurrentContext());
|
||||
if (!jsNumber.IsEmpty()) {
|
||||
v->setNumber(jsNumber.ToLocalChecked()->Value());
|
||||
v->setDouble(jsNumber.ToLocalChecked()->Value());
|
||||
} else {
|
||||
v->setUndefined();
|
||||
}
|
||||
} else if (jsval->IsBigInt()) {
|
||||
v8::MaybeLocal<v8::BigInt> jsBigInt = jsval->ToBigInt(isolate->GetCurrentContext());
|
||||
if (!jsBigInt.IsEmpty()) {
|
||||
auto bigInt = jsBigInt.ToLocalChecked();
|
||||
v->setInt64(bigInt->Int64Value());
|
||||
} else {
|
||||
v->setUndefined();
|
||||
}
|
||||
@@ -140,7 +150,9 @@ void setReturnValueTemplate(const Value &data, const T &argv) {
|
||||
} else if (data.getType() == Value::Type::Null) {
|
||||
argv.GetReturnValue().Set(v8::Null(argv.GetIsolate()));
|
||||
} else if (data.getType() == Value::Type::Number) {
|
||||
argv.GetReturnValue().Set(v8::Number::New(argv.GetIsolate(), data.toNumber()));
|
||||
argv.GetReturnValue().Set(v8::Number::New(argv.GetIsolate(), data.toDouble()));
|
||||
} else if (data.getType() == Value::Type::BigInt) {
|
||||
argv.GetReturnValue().Set(v8::BigInt::New(argv.GetIsolate(), data.toInt64()));
|
||||
} else if (data.getType() == Value::Type::String) {
|
||||
v8::MaybeLocal<v8::String> value = v8::String::NewFromUtf8(argv.GetIsolate(), data.toString().c_str(), v8::NewStringType::kNormal);
|
||||
assert(!value.IsEmpty());
|
||||
|
||||
@@ -437,7 +437,7 @@ bool JavaScriptJavaBridge::convertReturnValue(ReturnValue retValue, ValueType ty
|
||||
ret->setInt32(retValue.intValue);
|
||||
break;
|
||||
case JavaScriptJavaBridge::ValueType::LONG:
|
||||
ret->setLong(retValue.longValue); //NOLINT(bugprone-narrowing-conversions)
|
||||
ret->setDouble(static_cast<double>(retValue.longValue));
|
||||
break;
|
||||
case JavaScriptJavaBridge::ValueType::FLOAT:
|
||||
ret->setFloat(retValue.floatValue);
|
||||
|
||||
@@ -126,7 +126,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
int val = arg.toInt32();
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == @encode(long)) {
|
||||
long val = arg.toLong();
|
||||
long val = static_cast<long>(arg.toDouble());
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == @encode(short)) {
|
||||
short val = arg.toInt16();
|
||||
@@ -135,7 +135,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
unsigned int val = arg.toUint32();
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == @encode(unsigned long)) {
|
||||
unsigned long val = arg.toUlong();
|
||||
unsigned long val = static_cast<unsigned long>(arg.toDouble());
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == @encode(unsigned short)) {
|
||||
unsigned short val = arg.toUint16();
|
||||
@@ -144,7 +144,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
float val = arg.toFloat();
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == @encode(double)) {
|
||||
double val = arg.toNumber();
|
||||
double val = arg.toDouble();
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == @encode(char)) {
|
||||
char val = arg.toInt8();
|
||||
@@ -153,7 +153,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
unsigned char val = arg.toUint8();
|
||||
[invocation setArgument:&val atIndex:i];
|
||||
} else if (argumentType == "@") { // NSNumber*
|
||||
NSNumber *number = [NSNumber numberWithDouble:arg.toNumber()];
|
||||
NSNumber *number = [NSNumber numberWithDouble:arg.toDouble()];
|
||||
[invocation setArgument:&number atIndex:i];
|
||||
} else {
|
||||
NSLog(@"Unsupported argument type: %s", argumentType.c_str());
|
||||
@@ -201,7 +201,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
} else if (returnType == @encode(long)) {
|
||||
long ret;
|
||||
[invocation getReturnValue:&ret];
|
||||
rval.setLong(ret);
|
||||
rval.setDouble(static_cast<long>(ret));
|
||||
} else if (returnType == @encode(short)) {
|
||||
short ret;
|
||||
[invocation getReturnValue:&ret];
|
||||
@@ -213,7 +213,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
} else if (returnType == @encode(unsigned long)) {
|
||||
unsigned long ret;
|
||||
[invocation getReturnValue:&ret];
|
||||
rval.setUlong(ret);
|
||||
rval.setDouble(static_cast<double>(ret));
|
||||
} else if (returnType == @encode(unsigned short)) {
|
||||
unsigned short ret;
|
||||
[invocation getReturnValue:&ret];
|
||||
@@ -225,7 +225,7 @@ bool JavaScriptObjCBridge::CallInfo::execute(const se::ValueArray &argv, se::Val
|
||||
} else if (returnType == @encode(double)) {
|
||||
double ret;
|
||||
[invocation getReturnValue:&ret];
|
||||
rval.setNumber(ret);
|
||||
rval.setDouble(ret);
|
||||
} else if (returnType == @encode(char)) {
|
||||
int8_t ret;
|
||||
[invocation getReturnValue:&ret];
|
||||
@@ -260,7 +260,7 @@ se::Value JavaScriptObjCBridge::CallInfo::objc_to_seval(id objcVal) {
|
||||
if (numberType == @encode(BOOL) || numberType == @encode(bool)) {
|
||||
ret.setBoolean([number boolValue]);
|
||||
} else if (numberType == @encode(int) || numberType == @encode(long) || numberType == @encode(short) || numberType == @encode(unsigned int) || numberType == @encode(unsigned long) || numberType == @encode(unsigned short) || numberType == @encode(float) || numberType == @encode(double) || numberType == @encode(char) || numberType == @encode(unsigned char)) {
|
||||
ret.setNumber([number doubleValue]);
|
||||
ret.setDouble([number doubleValue]);
|
||||
} else {
|
||||
CC_LOG_ERROR("Unknown number type: %s", numberType.c_str());
|
||||
}
|
||||
|
||||
@@ -62,20 +62,6 @@ bool seval_to_uint32(const se::Value &v, uint32_t *ret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool seval_to_uint(const se::Value &v, unsigned int *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
*ret = v.toUint();
|
||||
return true;
|
||||
}
|
||||
if (v.isBoolean()) {
|
||||
*ret = v.toBoolean() ? 1 : 0;
|
||||
return true;
|
||||
}
|
||||
*ret = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool seval_to_int8(const se::Value &v, int8_t *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
@@ -166,7 +152,7 @@ bool seval_to_float(const se::Value &v, float *ret) {
|
||||
|
||||
bool seval_to_double(const se::Value &v, double *ret) {
|
||||
if (v.isNumber()) {
|
||||
*ret = v.toNumber();
|
||||
*ret = v.toDouble();
|
||||
if (!std::isnan(*ret)) {
|
||||
return true;
|
||||
}
|
||||
@@ -175,40 +161,10 @@ bool seval_to_double(const se::Value &v, double *ret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool seval_to_long(const se::Value &v, long *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
*ret = v.toLong();
|
||||
return true;
|
||||
}
|
||||
*ret = 0L;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool seval_to_ulong(const se::Value &v, unsigned long *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
*ret = v.toUlong();
|
||||
return true;
|
||||
}
|
||||
*ret = 0UL;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool seval_to_longlong(const se::Value &v, long long *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
*ret = static_cast<long long>(v.toLong());
|
||||
return true;
|
||||
}
|
||||
*ret = 0LL;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool seval_to_size(const se::Value &v, size_t *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
*ret = static_cast<size_t>(v.toLong());
|
||||
*ret = v.toSize();
|
||||
return true;
|
||||
}
|
||||
*ret = 0;
|
||||
@@ -277,7 +233,7 @@ bool seval_to_Uint8Array(const se::Value &v, uint8_t *ret) {
|
||||
bool seval_to_uintptr_t(const se::Value &v, uintptr_t *ret) {
|
||||
assert(ret != nullptr);
|
||||
if (v.isNumber()) {
|
||||
*ret = v.toUIntptr_t();
|
||||
*ret = static_cast<uintptr_t>(v.toDouble());
|
||||
return true;
|
||||
}
|
||||
*ret = 0UL;
|
||||
@@ -321,7 +277,7 @@ bool seval_to_ccvalue(const se::Value &v, cc::Value *ret) {
|
||||
} else if (v.isString()) {
|
||||
*ret = v.toString();
|
||||
} else if (v.isNumber()) {
|
||||
*ret = v.toNumber();
|
||||
*ret = v.toDouble();
|
||||
} else if (v.isBoolean()) {
|
||||
*ret = v.toBoolean();
|
||||
} else if (v.isNullOrUndefined()) {
|
||||
@@ -727,32 +683,17 @@ bool float_to_seval(float v, se::Value *ret) {
|
||||
}
|
||||
|
||||
bool double_to_seval(double v, se::Value *ret) {
|
||||
ret->setNumber(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool long_to_seval(long v, se::Value *ret) {
|
||||
ret->setLong(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ulong_to_seval(unsigned long v, se::Value *ret) {
|
||||
ret->setUlong(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool longlong_to_seval(long long v, se::Value *ret) {
|
||||
ret->setLong(static_cast<long>(v));
|
||||
ret->setDouble(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool uintptr_t_to_seval(uintptr_t v, se::Value *ret) {
|
||||
ret->setUIntptr_t(v);
|
||||
ret->setDouble(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool size_to_seval(size_t v, se::Value *ret) {
|
||||
ret->setLong(static_cast<unsigned long>(v));
|
||||
ret->setSize(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -842,7 +783,7 @@ bool ccvalue_to_seval(const cc::Value &v, se::Value *ret) {
|
||||
break;
|
||||
case cc::Value::Type::FLOAT:
|
||||
case cc::Value::Type::DOUBLE:
|
||||
ret->setNumber(v.asDouble());
|
||||
ret->setDouble(v.asDouble());
|
||||
break;
|
||||
case cc::Value::Type::INTEGER:
|
||||
ret->setInt32(v.asInt());
|
||||
|
||||
@@ -161,7 +161,6 @@ struct InputAssemblerInfo;
|
||||
// se value -> native value
|
||||
bool seval_to_int32(const se::Value &v, int32_t *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_uint32(const se::Value &v, uint32_t *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_uint(const se::Value &v, unsigned int *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_int8(const se::Value &v, int8_t *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_uint8(const se::Value &v, uint8_t *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_int16(const se::Value &v, int16_t *ret); // NOLINT(readability-identifier-naming)
|
||||
@@ -169,9 +168,6 @@ bool seval_to_uint16(const se::Value &v, uint16_t *ret);
|
||||
bool seval_to_boolean(const se::Value &v, bool *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_float(const se::Value &v, float *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_double(const se::Value &v, double *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_long(const se::Value &v, long *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_ulong(const se::Value &v, unsigned long *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_longlong(const se::Value &v, long long *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_size(const se::Value &v, size_t *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_std_string(const se::Value &v, std::string *ret); // NOLINT(readability-identifier-naming)
|
||||
bool seval_to_Vec2(const se::Value &v, cc::Vec2 *pt); // NOLINT(readability-identifier-naming)
|
||||
@@ -884,7 +880,7 @@ inline bool sevalue_to_native(const se::Value &from, std::string *to, se::Object
|
||||
///// integers
|
||||
template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, bool *to, se::Object * /*ctx*/) {
|
||||
*to = from.isNullOrUndefined() ? false : (from.isNumber() ? from.toNumber() != 0 : from.toBoolean());
|
||||
*to = from.isNullOrUndefined() ? false : (from.isNumber() ? from.toDouble() != 0 : from.toBoolean());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -923,20 +919,20 @@ inline bool sevalue_to_native(const se::Value &from, uint8_t *to, se::Object * /
|
||||
|
||||
template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, uint64_t *to, se::Object * /*ctx*/) {
|
||||
*to = from.toUIntptr_t();
|
||||
*to = from.toUint64();
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, int64_t *to, se::Object * /*ctx*/) {
|
||||
*to = static_cast<int64_t>(from.toNumber());
|
||||
*to = from.toInt64();
|
||||
return true;
|
||||
}
|
||||
|
||||
#if CC_PLATFORM == CC_PLATFORM_MAC_IOS || CC_PLATFORM == CC_PLATFORM_MAC_OSX
|
||||
template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, uintptr_t *to, se::Object *ctx) {
|
||||
*to = (uintptr_t)from.toNumber();
|
||||
inline bool sevalue_to_native(const se::Value &from, unsigned long *to, se::Object * /*ctx*/) {
|
||||
*to = static_cast<unsigned long>(from.toDouble());
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -947,7 +943,7 @@ inline bool sevalue_to_native(const se::Value &from, float *to, se::Object * /*c
|
||||
return true;
|
||||
}
|
||||
inline bool sevalue_to_native(const se::Value &from, double *to, se::Object * /*unused*/) {
|
||||
*to = from.toNumber();
|
||||
*to = from.toDouble();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1037,13 +1033,13 @@ sevalue_to_native(const se::Value &from, T to, se::Object * /*ctx*/) { // NOLINT
|
||||
} else if CC_CONSTEXPR (std::is_arithmetic<Value>::value) {
|
||||
se::Object *array = from.toObject();
|
||||
if (array->isTypedArray()) {
|
||||
uint8_t* data = nullptr;
|
||||
uint8_t *data = nullptr;
|
||||
array->getTypedArrayData(&data, nullptr);
|
||||
*to = reinterpret_cast<Value*>(data);
|
||||
*to = reinterpret_cast<Value *>(data);
|
||||
} else if (array->isArrayBuffer()) {
|
||||
uint8_t* data = nullptr;
|
||||
uint8_t *data = nullptr;
|
||||
array->getArrayBufferData(&data, nullptr);
|
||||
*to = reinterpret_cast<Value*>(data);
|
||||
*to = reinterpret_cast<Value *>(data);
|
||||
} else {
|
||||
assert(false);
|
||||
return false;
|
||||
@@ -1316,8 +1312,11 @@ inline bool nativevalue_to_se(const T &from, se::Value &to, se::Object *ctx) { /
|
||||
return native_ptr_to_seval(from, &to);
|
||||
} else if CC_CONSTEXPR (is_jsb_object_v<T>) {
|
||||
return native_ptr_to_seval(from, &to);
|
||||
} else if CC_CONSTEXPR (std::is_same<T, int64_t>::value || std::is_same<T, uint64_t>::value) {
|
||||
to.setInt64(static_cast<int64_t>(from));
|
||||
return true;
|
||||
} else if CC_CONSTEXPR (std::is_arithmetic<T>::value) {
|
||||
to.setNumber(static_cast<double>(from));
|
||||
to.setDouble(static_cast<double>(from));
|
||||
return true;
|
||||
} else {
|
||||
return nativevalue_to_se<typename std::conditional_t<std::is_const<T>::value, T, typename std::add_const<T>::type>>(from, to, ctx);
|
||||
@@ -1421,13 +1420,13 @@ inline bool nativevalue_to_se(const std::array<float, N> &from, se::Value &to, s
|
||||
|
||||
template <>
|
||||
inline bool nativevalue_to_se(const int64_t &from, se::Value &to, se::Object * /*ctx*/) {
|
||||
to.setLong(static_cast<long>(from));
|
||||
to.setInt64(from);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool nativevalue_to_se(const uint64_t &from, se::Value &to, se::Object * /*ctx*/) {
|
||||
to.setUlong(static_cast<unsigned long>(from));
|
||||
to.setUint64(from);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1465,18 +1464,6 @@ inline bool nativevalue_to_se(const uint8_t &from, se::Value &to, se::Object * /
|
||||
return true;
|
||||
}
|
||||
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
|
||||
template <>
|
||||
inline bool nativevalue_to_se(const long &from, se::Value &to, se::Object *ctx) {
|
||||
if (sizeof(long) == 4) {
|
||||
to.setInt32(from);
|
||||
} else {
|
||||
to.setNumber(static_cast<double>(from));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
template <>
|
||||
inline bool nativevalue_to_se(const std::string &from, se::Value &to, se::Object * /*ctx*/) {
|
||||
to.setString(from);
|
||||
|
||||
@@ -67,9 +67,7 @@ bool js_gfx_Device_copyBuffersToTexture(se::State &s) { // NOLINT(readability-id
|
||||
assert(false);
|
||||
}
|
||||
} else {
|
||||
unsigned long address = 0; //NOLINT(google-runtime-int)
|
||||
seval_to_ulong(value, &address);
|
||||
ptr = reinterpret_cast<uint8_t *>(address);
|
||||
ptr = reinterpret_cast<uint8_t *>(value.asPtr());
|
||||
}
|
||||
|
||||
arg0[i] = ptr;
|
||||
@@ -111,12 +109,11 @@ bool js_gfx_Device_copyTexImagesToTexture(se::State &s) { // NOLINT(readability-
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
if (dataObj->getArrayElement(i, &value)) {
|
||||
if (value.isObject()) {
|
||||
|
||||
uint8_t * address = nullptr;
|
||||
uint8_t *address = nullptr;
|
||||
value.toObject()->getTypedArrayData(&address, &dataLength);
|
||||
arg0[i] = address;
|
||||
} else {
|
||||
arg0[i] = reinterpret_cast<uint8_t *>(value.toUIntptr_t());
|
||||
arg0[i] = reinterpret_cast<uint8_t *>(value.asPtr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,7 +493,7 @@ static bool getOrCreatePlainObject_r(const char *name, se::Object *parent, se::O
|
||||
static bool js_performance_now(se::State &s) { //NOLINT
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto micro = std::chrono::duration_cast<std::chrono::microseconds>(now - se::ScriptEngine::getInstance()->getStartTime()).count();
|
||||
s.rval().setNumber(static_cast<double>(micro) * 0.001);
|
||||
s.rval().setDouble(static_cast<double>(micro) * 0.001);
|
||||
return true;
|
||||
}
|
||||
SE_BIND_FUNC(js_performance_now)
|
||||
@@ -626,7 +626,7 @@ bool jsb_global_load_image(const std::string &path, const se::Value &callbackVal
|
||||
|
||||
if (loadSucceed) {
|
||||
se::HandleObject retObj(se::Object::createPlainObject());
|
||||
dataVal.setUIntptr_t(reinterpret_cast<uintptr_t>(imgInfo->data));
|
||||
dataVal.setUint64(reinterpret_cast<uintptr_t>(imgInfo->data));
|
||||
retObj->setProperty("data", dataVal);
|
||||
retObj->setProperty("width", se::Value(imgInfo->width));
|
||||
retObj->setProperty("height", se::Value(imgInfo->height));
|
||||
@@ -698,7 +698,7 @@ static bool js_destroyImage(se::State &s) { //NOLINT
|
||||
size_t argc = args.size();
|
||||
CC_UNUSED bool ok = true;
|
||||
if (argc == 1) {
|
||||
auto *data = reinterpret_cast<char *>(args[0].toUIntptr_t());
|
||||
auto *data = reinterpret_cast<char *>(args[0].asPtr());
|
||||
SE_PRECONDITION2(ok, false, "js_destroyImage : Error processing arguments");
|
||||
free(data);
|
||||
return true;
|
||||
|
||||
@@ -30,23 +30,24 @@
|
||||
// Created by James Chen on 5/15/17.
|
||||
//
|
||||
//
|
||||
#include "base/Config.h"
|
||||
#include "jsb_xmlhttprequest.h"
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include "base/Config.h"
|
||||
#include "cocos/base/Data.h"
|
||||
#include "cocos/bindings/jswrapper/SeApi.h"
|
||||
#include "cocos/bindings/manual/jsb_conversions.h"
|
||||
#include "cocos/network/HttpClient.h"
|
||||
#include "cocos/base/Data.h"
|
||||
#include "platform/Application.h"
|
||||
|
||||
using namespace cc;
|
||||
using namespace cc::network;
|
||||
using namespace cc; //NOLINT
|
||||
using namespace cc::network; //NOLINT
|
||||
|
||||
namespace {
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
std::unordered_map<int, std::string> _httpStatusCodeMap = {
|
||||
{100, "Continue"},
|
||||
{101, "Switching Protocols"},
|
||||
@@ -111,17 +112,17 @@ std::unordered_map<int, std::string> _httpStatusCodeMap = {
|
||||
{510, "Not Extended"},
|
||||
{511, "Network Authentication Required"},
|
||||
{599, "Network Connect Timeout Error"}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class XMLHttpRequest : public Ref {
|
||||
public:
|
||||
// Ready States: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState
|
||||
enum class ReadyState : char {
|
||||
UNSENT = 0, // Client has been created. open() not called yet.
|
||||
OPENED = 1, // open() has been called.
|
||||
UNSENT = 0, // Client has been created. open() not called yet.
|
||||
OPENED = 1, // open() has been called.
|
||||
HEADERS_RECEIVED = 2, // send() has been called, and headers and status are available.
|
||||
LOADING = 3, // Downloading; responseText holds partial data.
|
||||
DONE = 4 // The operation is complete.
|
||||
LOADING = 3, // Downloading; responseText holds partial data.
|
||||
DONE = 4 // The operation is complete.
|
||||
};
|
||||
|
||||
enum class ResponseType : char {
|
||||
@@ -147,30 +148,30 @@ public:
|
||||
void sendString(const std::string &str);
|
||||
void sendBinary(const cc::Data &data);
|
||||
|
||||
void setRequestHeader(const std::string &key, const std::string &value);
|
||||
void setRequestHeader(const std::string &key, const std::string &value);
|
||||
std::string getAllResponseHeaders() const;
|
||||
std::string getResonpseHeader(const std::string &key) const;
|
||||
std::string getResponseHeader(const std::string &key) const;
|
||||
|
||||
ReadyState getReadyState() const { return _readyState; }
|
||||
uint16_t getStatus() const { return _status; }
|
||||
ReadyState getReadyState() const { return _readyState; }
|
||||
uint16_t getStatus() const { return _status; }
|
||||
const std::string &getStatusText() const { return _statusText; }
|
||||
const std::string &getResponseText() const { return _responseText; }
|
||||
const cc::Data &getResponseData() const { return _responseData; }
|
||||
ResponseType getResponseType() const { return _responseType; }
|
||||
void setResponseType(ResponseType type) { _responseType = type; }
|
||||
const cc::Data & getResponseData() const { return _responseData; }
|
||||
ResponseType getResponseType() const { return _responseType; }
|
||||
void setResponseType(ResponseType type) { _responseType = type; }
|
||||
|
||||
void overrideMimeType(const std::string &mimeType);
|
||||
void overrideMimeType(const std::string &mimeType);
|
||||
std::string getMimeType() const;
|
||||
|
||||
void setTimeout(unsigned long timeoutInMilliseconds);
|
||||
unsigned long getTimeout() const;
|
||||
void setTimeout(uint32_t timeoutInMilliseconds);
|
||||
uint32_t getTimeout() const;
|
||||
|
||||
void abort();
|
||||
|
||||
bool isDiscardedByReset() const { return _isDiscardedByReset; }
|
||||
|
||||
private:
|
||||
virtual ~XMLHttpRequest();
|
||||
~XMLHttpRequest() override;
|
||||
|
||||
void setReadyState(ReadyState readyState);
|
||||
void getHeader(const std::string &header);
|
||||
@@ -195,20 +196,20 @@ private:
|
||||
cc::network::HttpRequest *_httpRequest;
|
||||
// cc::EventListenerCustom* _resetDirectorListener;
|
||||
|
||||
unsigned long _timeoutInMilliseconds;
|
||||
uint16_t _status;
|
||||
uint32_t _timeoutInMilliseconds{};
|
||||
uint16_t _status{};
|
||||
|
||||
ResponseType _responseType;
|
||||
ReadyState _readyState;
|
||||
ResponseType _responseType{};
|
||||
ReadyState _readyState{};
|
||||
|
||||
bool _withCredentialsValue;
|
||||
bool _errorFlag;
|
||||
bool _isAborted;
|
||||
bool _isLoadStart;
|
||||
bool _isLoadEnd;
|
||||
bool _isDiscardedByReset;
|
||||
bool _isTimeout;
|
||||
bool _isSending;
|
||||
bool _withCredentialsValue{false};
|
||||
bool _errorFlag{false};
|
||||
bool _isAborted{false};
|
||||
bool _isLoadStart{false};
|
||||
bool _isLoadEnd{false};
|
||||
bool _isDiscardedByReset{false};
|
||||
bool _isTimeout{false};
|
||||
bool _isSending{false};
|
||||
};
|
||||
|
||||
XMLHttpRequest::XMLHttpRequest()
|
||||
@@ -220,18 +221,8 @@ XMLHttpRequest::XMLHttpRequest()
|
||||
onerror(nullptr),
|
||||
ontimeout(nullptr),
|
||||
_httpRequest(new (std::nothrow) HttpRequest()),
|
||||
_timeoutInMilliseconds(0UL),
|
||||
_status(0),
|
||||
_responseType(ResponseType::STRING),
|
||||
_readyState(ReadyState::UNSENT),
|
||||
_withCredentialsValue(false),
|
||||
_errorFlag(false),
|
||||
_isAborted(false),
|
||||
_isLoadStart(false),
|
||||
_isLoadEnd(false),
|
||||
_isDiscardedByReset(false),
|
||||
_isTimeout(false),
|
||||
_isSending(false) {
|
||||
_readyState(ReadyState::UNSENT) {
|
||||
}
|
||||
|
||||
XMLHttpRequest::~XMLHttpRequest() {
|
||||
@@ -242,31 +233,33 @@ XMLHttpRequest::~XMLHttpRequest() {
|
||||
}
|
||||
|
||||
bool XMLHttpRequest::open(const std::string &method, const std::string &url) {
|
||||
if (_readyState != ReadyState::UNSENT)
|
||||
if (_readyState != ReadyState::UNSENT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_method = method;
|
||||
_url = url;
|
||||
_url = url;
|
||||
|
||||
HttpRequest::Type requestType = HttpRequest::Type::UNKNOWN;
|
||||
|
||||
if (_method == "get" || _method == "GET")
|
||||
if (_method == "get" || _method == "GET") {
|
||||
requestType = HttpRequest::Type::GET;
|
||||
else if (_method == "post" || _method == "POST")
|
||||
} else if (_method == "post" || _method == "POST") {
|
||||
requestType = HttpRequest::Type::POST;
|
||||
else if (_method == "put" || _method == "PUT")
|
||||
} else if (_method == "put" || _method == "PUT") {
|
||||
requestType = HttpRequest::Type::PUT;
|
||||
else if (_method == "head" || _method == "HEAD")
|
||||
} else if (_method == "head" || _method == "HEAD") {
|
||||
requestType = HttpRequest::Type::HEAD;
|
||||
else if (_method == "delete" || _method == "DELETE")
|
||||
} else if (_method == "delete" || _method == "DELETE") {
|
||||
requestType = HttpRequest::Type::DELETE;
|
||||
}
|
||||
|
||||
CCASSERT(requestType != HttpRequest::Type::UNKNOWN, (std::string("XMLHttpRequest.open: Unknown method: ") + method).c_str());
|
||||
|
||||
_httpRequest->setRequestType(requestType);
|
||||
_httpRequest->setUrl(_url);
|
||||
|
||||
_status = 0;
|
||||
_status = 0;
|
||||
_isAborted = false;
|
||||
_isTimeout = false;
|
||||
|
||||
@@ -285,22 +278,23 @@ void XMLHttpRequest::sendString(const std::string &str) {
|
||||
}
|
||||
|
||||
void XMLHttpRequest::sendBinary(const Data &data) {
|
||||
setHttpRequestData((const char *)data.getBytes(), data.getSize());
|
||||
setHttpRequestData(reinterpret_cast<const char *>(data.getBytes()), data.getSize());
|
||||
sendRequest();
|
||||
}
|
||||
|
||||
void XMLHttpRequest::setTimeout(unsigned long timeoutInMilliseconds) {
|
||||
void XMLHttpRequest::setTimeout(uint32_t timeoutInMilliseconds) {
|
||||
_timeoutInMilliseconds = timeoutInMilliseconds;
|
||||
_httpRequest->setTimeout(timeoutInMilliseconds / 1000.0f + 2.0f); // Add 2 seconds more to ensure the timeout scheduler is invoked before http response.
|
||||
_httpRequest->setTimeout(timeoutInMilliseconds / 1000.0F + 2.0F); // Add 2 seconds more to ensure the timeout scheduler is invoked before http response.
|
||||
}
|
||||
|
||||
unsigned long XMLHttpRequest::getTimeout() const {
|
||||
return _httpRequest->getTimeout() * 1000.0f;
|
||||
uint32_t XMLHttpRequest::getTimeout() const {
|
||||
return _httpRequest->getTimeout() * 1000;
|
||||
}
|
||||
|
||||
void XMLHttpRequest::abort() {
|
||||
if (!_isLoadStart)
|
||||
if (!_isLoadStart) {
|
||||
return;
|
||||
}
|
||||
|
||||
_isAborted = true;
|
||||
_isSending = false;
|
||||
@@ -333,12 +327,12 @@ void XMLHttpRequest::setReadyState(ReadyState readyState) {
|
||||
|
||||
void XMLHttpRequest::getHeader(const std::string &header) {
|
||||
// check for colon.
|
||||
size_t found_header_field = header.find_first_of(":");
|
||||
size_t found_header_field = header.find_first_of(':'); // NOLINT(readability-identifier-naming)
|
||||
|
||||
if (found_header_field != std::string::npos) {
|
||||
// Found a header field.
|
||||
std::string http_field;
|
||||
std::string http_value;
|
||||
std::string http_field; // NOLINT(readability-identifier-naming)
|
||||
std::string http_value; // NOLINT(readability-identifier-naming)
|
||||
|
||||
http_field = header.substr(0, found_header_field);
|
||||
http_value = header.substr(found_header_field + 1, header.length());
|
||||
@@ -362,7 +356,9 @@ void XMLHttpRequest::getHeader(const std::string &header) {
|
||||
// Get Header and Set StatusText
|
||||
// Split String into Tokens
|
||||
if (header.find("HTTP") == 0) {
|
||||
int _v1, _v2, code = 0;
|
||||
int _v1; // NOLINT(readability-identifier-naming)
|
||||
int _v2; // NOLINT(readability-identifier-naming)
|
||||
int code = 0;
|
||||
char statusText[64] = {0};
|
||||
sscanf(header.c_str(), "HTTP/%d.%d %d %63[^\n]", &_v1, &_v2, &code, statusText);
|
||||
_statusText = statusText;
|
||||
@@ -378,7 +374,7 @@ void XMLHttpRequest::getHeader(const std::string &header) {
|
||||
}
|
||||
}
|
||||
|
||||
void XMLHttpRequest::onResponse(HttpClient *client, HttpResponse *response) {
|
||||
void XMLHttpRequest::onResponse(HttpClient * /*client*/, HttpResponse *response) {
|
||||
Application::getInstance()->getScheduler()->unscheduleAllForTarget(this);
|
||||
_isSending = false;
|
||||
|
||||
@@ -399,7 +395,7 @@ void XMLHttpRequest::onResponse(HttpClient *client, HttpResponse *response) {
|
||||
SE_LOGD("XMLHttpRequest::onResponse, %s completed\n", tag.c_str());
|
||||
}
|
||||
|
||||
long statusCode = response->getResponseCode();
|
||||
auto statusCode = response->getResponseCode();
|
||||
char statusString[64] = {0};
|
||||
sprintf(statusString, "HTTP Status Code: %ld, tag = %s", statusCode, tag.c_str());
|
||||
|
||||
@@ -411,7 +407,7 @@ void XMLHttpRequest::onResponse(HttpClient *client, HttpResponse *response) {
|
||||
SE_LOGD("Response failed, error buffer: %s\n", errorBuffer.c_str());
|
||||
if (statusCode == 0 || statusCode == -1) {
|
||||
_errorFlag = true;
|
||||
_status = 0;
|
||||
_status = 0;
|
||||
_statusText.clear();
|
||||
if (onerror != nullptr) {
|
||||
onerror();
|
||||
@@ -431,7 +427,7 @@ void XMLHttpRequest::onResponse(HttpClient *client, HttpResponse *response) {
|
||||
std::string header(headers->begin(), headers->end());
|
||||
|
||||
std::istringstream stream(header);
|
||||
std::string line;
|
||||
std::string line;
|
||||
while (std::getline(stream, line)) {
|
||||
getHeader(line);
|
||||
}
|
||||
@@ -442,7 +438,7 @@ void XMLHttpRequest::onResponse(HttpClient *client, HttpResponse *response) {
|
||||
if (_responseType == ResponseType::STRING || _responseType == ResponseType::JSON) {
|
||||
_responseText.append(buffer->data(), buffer->size());
|
||||
} else {
|
||||
_responseData.copy((unsigned char *)buffer->data(), buffer->size());
|
||||
_responseData.copy(reinterpret_cast<unsigned char *>(buffer->data()), buffer->size());
|
||||
}
|
||||
|
||||
_status = statusCode;
|
||||
@@ -467,30 +463,31 @@ std::string XMLHttpRequest::getMimeType() const {
|
||||
if (!_overrideMimeType.empty()) {
|
||||
return _overrideMimeType;
|
||||
}
|
||||
auto contentType = getResonpseHeader("Content-Type");
|
||||
auto contentType = getResponseHeader("Content-Type");
|
||||
return contentType.empty() ? "text" : contentType;
|
||||
}
|
||||
|
||||
void XMLHttpRequest::sendRequest() {
|
||||
if (_isSending) {
|
||||
//ref https://xhr.spec.whatwg.org/#the-send()-method
|
||||
//TODO: if send() flag is set, an exception should be thrown out.
|
||||
// TODO(unknown): if send() flag is set, an exception should be thrown out.
|
||||
return;
|
||||
}
|
||||
_isSending = true;
|
||||
_isTimeout = false;
|
||||
if (_timeoutInMilliseconds > 0) {
|
||||
Application::getInstance()->getScheduler()->schedule([this](float dt) {
|
||||
if (ontimeout != nullptr)
|
||||
Application::getInstance()->getScheduler()->schedule([this](float /* dt */) {
|
||||
if (ontimeout != nullptr) {
|
||||
ontimeout();
|
||||
_isTimeout = true;
|
||||
}
|
||||
_isTimeout = true;
|
||||
_readyState = ReadyState::UNSENT;
|
||||
},
|
||||
this, _timeoutInMilliseconds / 1000.0f, 0, 0.0f, false, "XMLHttpRequest");
|
||||
this, _timeoutInMilliseconds / 1000.0F, 0, 0.0F, false, "XMLHttpRequest");
|
||||
}
|
||||
setHttpRequestHeader();
|
||||
|
||||
_httpRequest->setResponseCallback(CC_CALLBACK_2(XMLHttpRequest::onResponse, this));
|
||||
_httpRequest->setResponseCallback(CC_CALLBACK_2(XMLHttpRequest::onResponse, this)); //NOLINT
|
||||
cc::network::HttpClient::getInstance()->sendImmediate(_httpRequest);
|
||||
|
||||
if (onloadstart != nullptr) {
|
||||
@@ -508,9 +505,9 @@ void XMLHttpRequest::setHttpRequestData(const char *data, size_t len) {
|
||||
}
|
||||
|
||||
void XMLHttpRequest::setRequestHeader(const std::string &key, const std::string &value) {
|
||||
std::stringstream header_s;
|
||||
std::stringstream value_s;
|
||||
std::string header;
|
||||
std::stringstream header_s; // NOLINT(readability-identifier-naming)
|
||||
std::stringstream value_s; // NOLINT(readability-identifier-naming)
|
||||
std::string header;
|
||||
|
||||
auto iter = _requestHeader.find(key);
|
||||
|
||||
@@ -526,17 +523,17 @@ void XMLHttpRequest::setRequestHeader(const std::string &key, const std::string
|
||||
|
||||
std::string XMLHttpRequest::getAllResponseHeaders() const {
|
||||
std::stringstream responseheaders;
|
||||
std::string responseheader;
|
||||
std::string responseheader;
|
||||
|
||||
for (auto it = _httpHeader.begin(); it != _httpHeader.end(); ++it) {
|
||||
responseheaders << it->first << ": " << it->second << "\n";
|
||||
for (const auto &it : _httpHeader) {
|
||||
responseheaders << it.first << ": " << it.second << "\n";
|
||||
}
|
||||
|
||||
responseheader = responseheaders.str();
|
||||
return responseheader;
|
||||
}
|
||||
|
||||
std::string XMLHttpRequest::getResonpseHeader(const std::string &key) const {
|
||||
std::string XMLHttpRequest::getResponseHeader(const std::string &key) const {
|
||||
std::string ret;
|
||||
std::string value = key;
|
||||
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
|
||||
@@ -551,18 +548,18 @@ std::string XMLHttpRequest::getResonpseHeader(const std::string &key) const {
|
||||
void XMLHttpRequest::setHttpRequestHeader() {
|
||||
std::vector<std::string> header;
|
||||
|
||||
for (auto it = _requestHeader.begin(); it != _requestHeader.end(); ++it) {
|
||||
const char *first = it->first.c_str();
|
||||
const char *second = it->second.c_str();
|
||||
size_t len = sizeof(char) * (strlen(first) + 3 + strlen(second));
|
||||
char *test = (char *)malloc(len);
|
||||
for (auto &it : _requestHeader) {
|
||||
const char *first = it.first.c_str();
|
||||
const char *second = it.second.c_str();
|
||||
size_t len = sizeof(char) * (strlen(first) + 3 + strlen(second));
|
||||
char * test = static_cast<char *>(malloc(len));
|
||||
memset(test, 0, len);
|
||||
|
||||
strcpy(test, first);
|
||||
strcpy(test + strlen(first), ": ");
|
||||
strcpy(test + strlen(first) + 2, second);
|
||||
|
||||
header.push_back(test);
|
||||
header.emplace_back(test);
|
||||
|
||||
free(test);
|
||||
}
|
||||
@@ -572,10 +569,10 @@ void XMLHttpRequest::setHttpRequestHeader() {
|
||||
}
|
||||
}
|
||||
|
||||
se::Class *__jsb_XMLHttpRequest_class = nullptr;
|
||||
se::Class *__jsb_XMLHttpRequest_class = nullptr; //NOLINT(readability-identifier-naming, bugprone-reserved-identifier)
|
||||
|
||||
static bool XMLHttpRequest_finalize(se::State &s) {
|
||||
XMLHttpRequest *request = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_finalize(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *request = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
SE_LOGD("XMLHttpRequest_finalize, %p ... \n", request);
|
||||
if (request->getReferenceCount() == 1) {
|
||||
request->autorelease();
|
||||
@@ -586,8 +583,8 @@ static bool XMLHttpRequest_finalize(se::State &s) {
|
||||
}
|
||||
SE_BIND_FINALIZE_FUNC(XMLHttpRequest_finalize)
|
||||
|
||||
static bool XMLHttpRequest_constructor(se::State &s) {
|
||||
XMLHttpRequest *request = new XMLHttpRequest();
|
||||
static bool XMLHttpRequest_constructor(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *request = new XMLHttpRequest();
|
||||
s.thisObject()->setPrivateData(request);
|
||||
|
||||
se::Value thiz(s.thisObject());
|
||||
@@ -649,12 +646,12 @@ static bool XMLHttpRequest_constructor(se::State &s) {
|
||||
}
|
||||
SE_BIND_CTOR(XMLHttpRequest_constructor, __jsb_XMLHttpRequest_class, XMLHttpRequest_finalize)
|
||||
|
||||
static bool XMLHttpRequest_open(se::State &s) {
|
||||
static bool XMLHttpRequest_open(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
int argc = (int)args.size();
|
||||
int argc = static_cast<int>(args.size());
|
||||
if (argc >= 2) {
|
||||
XMLHttpRequest *request = (XMLHttpRequest *)s.nativeThisObject();
|
||||
bool ok = false;
|
||||
auto * request = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
bool ok = false;
|
||||
std::string method;
|
||||
ok = seval_to_std_string(args[0], &method);
|
||||
SE_PRECONDITION2(ok, false, "args[0] isn't a string.");
|
||||
@@ -671,17 +668,17 @@ static bool XMLHttpRequest_open(se::State &s) {
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_open)
|
||||
|
||||
static bool XMLHttpRequest_abort(se::State &s) {
|
||||
XMLHttpRequest *request = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_abort(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *request = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
request->abort();
|
||||
return true;
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_abort)
|
||||
|
||||
static bool XMLHttpRequest_send(se::State &s) {
|
||||
const auto &args = s.args();
|
||||
size_t argc = args.size();
|
||||
XMLHttpRequest *request = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_send(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
size_t argc = args.size();
|
||||
auto * request = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
|
||||
if (argc == 0) {
|
||||
request->send();
|
||||
@@ -696,7 +693,7 @@ static bool XMLHttpRequest_send(se::State &s) {
|
||||
|
||||
if (obj->isTypedArray()) {
|
||||
uint8_t *ptr = nullptr;
|
||||
size_t len = 0;
|
||||
size_t len = 0;
|
||||
if (obj->getTypedArrayData(&ptr, &len)) {
|
||||
Data data;
|
||||
data.copy(ptr, len);
|
||||
@@ -707,7 +704,7 @@ static bool XMLHttpRequest_send(se::State &s) {
|
||||
}
|
||||
} else if (obj->isArrayBuffer()) {
|
||||
uint8_t *ptr = nullptr;
|
||||
size_t len = 0;
|
||||
size_t len = 0;
|
||||
if (obj->getArrayBufferData(&ptr, &len)) {
|
||||
Data data;
|
||||
data.copy(ptr, len);
|
||||
@@ -722,10 +719,11 @@ static bool XMLHttpRequest_send(se::State &s) {
|
||||
}
|
||||
} else {
|
||||
const char *typeName = "UNKNOWN";
|
||||
if (arg0.isBoolean())
|
||||
if (arg0.isBoolean()) {
|
||||
typeName = "boolean";
|
||||
else if (arg0.isNumber())
|
||||
} else if (arg0.isNumber()) {
|
||||
typeName = "number";
|
||||
}
|
||||
|
||||
SE_REPORT_ERROR("args[0] type: %s isn't supported!", typeName);
|
||||
return false;
|
||||
@@ -736,14 +734,14 @@ static bool XMLHttpRequest_send(se::State &s) {
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_send)
|
||||
|
||||
static bool XMLHttpRequest_setRequestHeader(se::State &s) {
|
||||
static bool XMLHttpRequest_setRequestHeader(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
size_t argc = args.size();
|
||||
size_t argc = args.size();
|
||||
|
||||
if (argc >= 2) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
auto * xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
std::string key;
|
||||
bool ok = seval_to_std_string(args[0], &key);
|
||||
bool ok = seval_to_std_string(args[0], &key);
|
||||
SE_PRECONDITION2(ok, false, "args[0] couldn't be converted to string.");
|
||||
std::string value;
|
||||
ok = seval_to_std_string(args[1], &value);
|
||||
@@ -757,23 +755,23 @@ static bool XMLHttpRequest_setRequestHeader(se::State &s) {
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_setRequestHeader)
|
||||
|
||||
static bool XMLHttpRequest_getAllResponseHeaders(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_getAllResponseHeaders(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto * xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
std::string headers = xhr->getAllResponseHeaders();
|
||||
s.rval().setString(headers);
|
||||
return true;
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_getAllResponseHeaders)
|
||||
|
||||
static bool XMLHttpRequest_getResonpseHeader(se::State &s) {
|
||||
static bool XMLHttpRequest_getResonpseHeader(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
size_t argc = args.size();
|
||||
size_t argc = args.size();
|
||||
if (argc > 0) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
auto * xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
std::string key;
|
||||
bool ok = seval_to_std_string(args[0], &key);
|
||||
bool ok = seval_to_std_string(args[0], &key);
|
||||
SE_PRECONDITION2(ok, false, "args[0] couldn't be converted to string.");
|
||||
std::string header = xhr->getResonpseHeader(key);
|
||||
std::string header = xhr->getResponseHeader(key);
|
||||
s.rval().setString(header);
|
||||
return true;
|
||||
}
|
||||
@@ -783,22 +781,22 @@ static bool XMLHttpRequest_getResonpseHeader(se::State &s) {
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_getResonpseHeader)
|
||||
|
||||
static bool XMLHttpRequest_overrideMimeType(se::State &s) {
|
||||
static bool XMLHttpRequest_overrideMimeType(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
int argc = (int)args.size();
|
||||
int argc = static_cast<int>(args.size());
|
||||
if (argc > 0 && args[0].isString()) {
|
||||
std::string mimeType;
|
||||
seval_to_std_string(args[0], &mimeType);
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
xhr->overrideMimeType(mimeType);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
SE_BIND_FUNC(XMLHttpRequest_overrideMimeType)
|
||||
|
||||
static bool XMLHttpRequest_getMIMEType(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
auto type = xhr->getMimeType();
|
||||
static bool XMLHttpRequest_getMIMEType(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
auto type = xhr->getMimeType();
|
||||
s.rval().setString(type);
|
||||
return true;
|
||||
}
|
||||
@@ -806,35 +804,35 @@ SE_BIND_PROP_GET(XMLHttpRequest_getMIMEType)
|
||||
|
||||
// getter
|
||||
|
||||
static bool XMLHttpRequest_getReadyState(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
s.rval().setInt32((int32_t)xhr->getReadyState());
|
||||
static bool XMLHttpRequest_getReadyState(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
s.rval().setInt32(static_cast<int32_t>(xhr->getReadyState()));
|
||||
return true;
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getReadyState)
|
||||
|
||||
static bool XMLHttpRequest_getStatus(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
s.rval().setInt32((int32_t)xhr->getStatus());
|
||||
static bool XMLHttpRequest_getStatus(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
s.rval().setUint16(xhr->getStatus());
|
||||
return true;
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getStatus)
|
||||
|
||||
static bool XMLHttpRequest_getStatusText(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_getStatusText(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
s.rval().setString(xhr->getStatusText());
|
||||
return true;
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getStatusText)
|
||||
|
||||
static bool XMLHttpRequest_getResponseText(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_getResponseText(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
s.rval().setString(xhr->getResponseText());
|
||||
return true;
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getResponseText)
|
||||
|
||||
static bool XMLHttpRequest_getResponseXML(se::State &s) {
|
||||
static bool XMLHttpRequest_getResponseXML(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
// DOM API is not fully supported in cocos2d-x-lite.
|
||||
// `.responseXML` requires a document object that is not possible to fulfill.
|
||||
s.rval().setNull();
|
||||
@@ -842,8 +840,8 @@ static bool XMLHttpRequest_getResponseXML(se::State &s) {
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getResponseXML)
|
||||
|
||||
static bool XMLHttpRequest_getResponse(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_getResponse(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
|
||||
if (xhr->getResponseType() == XMLHttpRequest::ResponseType::STRING) {
|
||||
s.rval().setString(xhr->getResponseText());
|
||||
@@ -853,14 +851,14 @@ static bool XMLHttpRequest_getResponse(se::State &s) {
|
||||
} else {
|
||||
if (xhr->getResponseType() == XMLHttpRequest::ResponseType::JSON) {
|
||||
const std::string &jsonText = xhr->getResponseText();
|
||||
se::HandleObject seObj(se::Object::createJSONObject(jsonText));
|
||||
se::HandleObject seObj(se::Object::createJSONObject(jsonText));
|
||||
if (!seObj.isEmpty()) {
|
||||
s.rval().setObject(seObj);
|
||||
} else {
|
||||
s.rval().setNull();
|
||||
}
|
||||
} else if (xhr->getResponseType() == XMLHttpRequest::ResponseType::ARRAY_BUFFER) {
|
||||
const Data &data = xhr->getResponseData();
|
||||
const Data & data = xhr->getResponseData();
|
||||
se::HandleObject seObj(se::Object::createArrayBufferObject(data.getBytes(), data.getSize()));
|
||||
if (!seObj.isEmpty()) {
|
||||
s.rval().setObject(seObj);
|
||||
@@ -876,20 +874,20 @@ static bool XMLHttpRequest_getResponse(se::State &s) {
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getResponse)
|
||||
|
||||
static bool XMLHttpRequest_getTimeout(se::State &s) {
|
||||
XMLHttpRequest *cobj = (XMLHttpRequest *)s.nativeThisObject();
|
||||
s.rval().setUlong(cobj->getTimeout());
|
||||
static bool XMLHttpRequest_getTimeout(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *cobj = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
s.rval().setUint32(cobj->getTimeout());
|
||||
return true;
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getTimeout)
|
||||
|
||||
static bool XMLHttpRequest_setTimeout(se::State &s) {
|
||||
static bool XMLHttpRequest_setTimeout(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
int argc = (int)args.size();
|
||||
int argc = static_cast<int>(args.size());
|
||||
if (argc > 0) {
|
||||
XMLHttpRequest *cobj = (XMLHttpRequest *)s.nativeThisObject();
|
||||
unsigned long timeoutInMilliseconds = 0;
|
||||
bool ok = seval_to_ulong(args[0], &timeoutInMilliseconds);
|
||||
auto * cobj = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
uint32_t timeoutInMilliseconds = 0;
|
||||
bool ok = seval_to_uint32(args[0], &timeoutInMilliseconds);
|
||||
SE_PRECONDITION2(ok, false, "args[0] isn't a number");
|
||||
if (timeoutInMilliseconds < 50) {
|
||||
SE_LOGE("The timeout value (%lu ms) is too small, please note that timeout unit is milliseconds!", timeoutInMilliseconds);
|
||||
@@ -903,8 +901,8 @@ static bool XMLHttpRequest_setTimeout(se::State &s) {
|
||||
}
|
||||
SE_BIND_PROP_SET(XMLHttpRequest_setTimeout)
|
||||
|
||||
static bool XMLHttpRequest_getResponseType(se::State &s) {
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
static bool XMLHttpRequest_getResponseType(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
switch (xhr->getResponseType()) {
|
||||
case XMLHttpRequest::ResponseType::STRING:
|
||||
s.rval().setString("text");
|
||||
@@ -922,16 +920,16 @@ static bool XMLHttpRequest_getResponseType(se::State &s) {
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getResponseType)
|
||||
|
||||
static bool XMLHttpRequest_setResponseType(se::State &s) {
|
||||
static bool XMLHttpRequest_setResponseType(se::State &s) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
const auto &args = s.args();
|
||||
size_t argc = args.size();
|
||||
size_t argc = args.size();
|
||||
|
||||
if (argc > 0) {
|
||||
std::string type;
|
||||
bool ok = seval_to_std_string(args[0], &type);
|
||||
bool ok = seval_to_std_string(args[0], &type);
|
||||
SE_PRECONDITION2(ok, false, "args[0] couldn't be converted to string!");
|
||||
|
||||
XMLHttpRequest *xhr = (XMLHttpRequest *)s.nativeThisObject();
|
||||
auto *xhr = static_cast<XMLHttpRequest *>(s.nativeThisObject());
|
||||
if (type == "text") {
|
||||
xhr->setResponseType(XMLHttpRequest::ResponseType::STRING);
|
||||
} else if (type == "arraybuffer") {
|
||||
@@ -953,13 +951,13 @@ static bool XMLHttpRequest_setResponseType(se::State &s) {
|
||||
}
|
||||
SE_BIND_PROP_SET(XMLHttpRequest_setResponseType)
|
||||
|
||||
static bool XMLHttpRequest_getWithCredentials(se::State &s) {
|
||||
static bool XMLHttpRequest_getWithCredentials(se::State & /*s*/) { //NOLINT(readability-identifier-naming, google-runtime-references)
|
||||
SE_LOGD("XMLHttpRequest.withCredentials isn't implemented on JSB!");
|
||||
return true;
|
||||
}
|
||||
SE_BIND_PROP_GET(XMLHttpRequest_getWithCredentials)
|
||||
|
||||
bool register_all_xmlhttprequest(se::Object *global) {
|
||||
bool register_all_xmlhttprequest(se::Object *global) { //NOLINT(readability-identifier-naming)
|
||||
se::Class *cls = se::Class::create("XMLHttpRequest", global, nullptr, _SE(XMLHttpRequest_constructor));
|
||||
cls->defineFinalizeFunction(_SE(XMLHttpRequest_finalize));
|
||||
|
||||
|
||||
@@ -29,4 +29,4 @@ namespace se {
|
||||
class Object;
|
||||
}
|
||||
|
||||
bool register_all_xmlhttprequest(se::Object *global);
|
||||
bool register_all_xmlhttprequest(se::Object *global); // NOLINT (readability-identifier-naming)
|
||||
|
||||
@@ -25,25 +25,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "bindings/manual/jsb_conversions.h"
|
||||
#include "base/TypeDef.h"
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "base/TypeDef.h"
|
||||
#include "bindings/manual/jsb_conversions.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
enum class ETouchState : uint8_t {
|
||||
ENTER = 0,
|
||||
STAY = 1,
|
||||
EXIT = 2,
|
||||
STAY = 1,
|
||||
EXIT = 2,
|
||||
};
|
||||
|
||||
struct TriggerEventPair {
|
||||
uintptr_t shapeA;
|
||||
uintptr_t shapeB;
|
||||
ETouchState state;
|
||||
uintptr_t shapeA;
|
||||
uintptr_t shapeB;
|
||||
ETouchState state;
|
||||
static constexpr uint8_t COUNT = 3;
|
||||
TriggerEventPair(const uintptr_t a, const uintptr_t b)
|
||||
: shapeA(a),
|
||||
@@ -52,21 +52,21 @@ struct TriggerEventPair {
|
||||
};
|
||||
|
||||
struct ContactPoint {
|
||||
Vec3 position;
|
||||
float separation;
|
||||
Vec3 normal;
|
||||
uint32_t internalFaceIndex0;
|
||||
Vec3 impulse;
|
||||
uint32_t internalFaceIndex1;
|
||||
Vec3 position;
|
||||
float separation;
|
||||
Vec3 normal;
|
||||
uint32_t internalFaceIndex0;
|
||||
Vec3 impulse;
|
||||
uint32_t internalFaceIndex1;
|
||||
static constexpr uint8_t COUNT = 12;
|
||||
};
|
||||
|
||||
struct ContactEventPair {
|
||||
uintptr_t shapeA;
|
||||
uintptr_t shapeB;
|
||||
ETouchState state;
|
||||
uintptr_t shapeA;
|
||||
uintptr_t shapeB;
|
||||
ETouchState state;
|
||||
std::vector<ContactPoint> contacts;
|
||||
static constexpr uint8_t COUNT = 4;
|
||||
static constexpr uint8_t COUNT = 4;
|
||||
ContactEventPair(const uintptr_t a, const uintptr_t b)
|
||||
: shapeA(a),
|
||||
shapeB(b),
|
||||
@@ -74,60 +74,61 @@ struct ContactEventPair {
|
||||
};
|
||||
|
||||
struct ConvexDesc {
|
||||
void *positions;
|
||||
void * positions;
|
||||
uint32_t positionLength;
|
||||
};
|
||||
|
||||
struct TrimeshDesc : ConvexDesc {
|
||||
void *triangles;
|
||||
void * triangles;
|
||||
uint32_t triangleLength;
|
||||
bool isU16;
|
||||
bool isU16;
|
||||
};
|
||||
|
||||
struct HeightFieldDesc {
|
||||
uint32_t rows;
|
||||
uint32_t columns;
|
||||
void *samples;
|
||||
void * samples;
|
||||
};
|
||||
|
||||
struct RaycastOptions {
|
||||
cc::Vec3 origin;
|
||||
float distance;
|
||||
float distance;
|
||||
cc::Vec3 unitDir;
|
||||
uint32_t mask;
|
||||
bool queryTrigger;
|
||||
bool queryTrigger;
|
||||
};
|
||||
|
||||
struct RaycastResult {
|
||||
uintptr_t shape{0};
|
||||
cc::Vec3 hitPoint;
|
||||
float distance;
|
||||
cc::Vec3 hitNormal;
|
||||
cc::Vec3 hitPoint;
|
||||
float distance;
|
||||
cc::Vec3 hitNormal;
|
||||
RaycastResult() = default;
|
||||
};
|
||||
|
||||
class IPhysicsWorld {
|
||||
public:
|
||||
virtual ~IPhysicsWorld()= default;;
|
||||
virtual void setGravity(float x, float y, float z) = 0;
|
||||
virtual void setAllowSleep(bool v) = 0;
|
||||
virtual void step(float s) = 0;
|
||||
virtual void emitEvents() = 0;
|
||||
virtual void syncSceneToPhysics() = 0;
|
||||
virtual void syncSceneWithCheck() = 0;
|
||||
virtual void destroy() = 0;
|
||||
virtual void setCollisionMatrix(uint32_t i, uint32_t m) = 0;
|
||||
virtual std::vector<std::shared_ptr<TriggerEventPair>> &getTriggerEventPairs() = 0;
|
||||
virtual std::vector<std::shared_ptr<ContactEventPair>> &getContactEventPairs() = 0;
|
||||
virtual bool raycast(RaycastOptions &opt) = 0;
|
||||
virtual bool raycastClosest(RaycastOptions &opt) = 0;
|
||||
virtual std::vector<RaycastResult> &raycastResult() = 0;
|
||||
virtual RaycastResult &raycastClosestResult() = 0;
|
||||
virtual uintptr_t createConvex(ConvexDesc &desc) = 0;
|
||||
virtual uintptr_t createTrimesh(TrimeshDesc &desc) = 0;
|
||||
virtual uintptr_t createHeightField(HeightFieldDesc &desc) = 0;
|
||||
virtual uintptr_t createMaterial(uint16_t id, float f, float df, float r,
|
||||
uint8_t m0, uint8_t m1) = 0;
|
||||
virtual ~IPhysicsWorld() = default;
|
||||
;
|
||||
virtual void setGravity(float x, float y, float z) = 0;
|
||||
virtual void setAllowSleep(bool v) = 0;
|
||||
virtual void step(float s) = 0;
|
||||
virtual void emitEvents() = 0;
|
||||
virtual void syncSceneToPhysics() = 0;
|
||||
virtual void syncSceneWithCheck() = 0;
|
||||
virtual void destroy() = 0;
|
||||
virtual void setCollisionMatrix(uint32_t i, uint32_t m) = 0;
|
||||
virtual std::vector<std::shared_ptr<TriggerEventPair>> &getTriggerEventPairs() = 0;
|
||||
virtual std::vector<std::shared_ptr<ContactEventPair>> &getContactEventPairs() = 0;
|
||||
virtual bool raycast(RaycastOptions &opt) = 0;
|
||||
virtual bool raycastClosest(RaycastOptions &opt) = 0;
|
||||
virtual std::vector<RaycastResult> & raycastResult() = 0;
|
||||
virtual RaycastResult & raycastClosestResult() = 0;
|
||||
virtual uintptr_t createConvex(ConvexDesc &desc) = 0;
|
||||
virtual uintptr_t createTrimesh(TrimeshDesc &desc) = 0;
|
||||
virtual uintptr_t createHeightField(HeightFieldDesc &desc) = 0;
|
||||
virtual uintptr_t createMaterial(uint16_t id, float f, float df, float r,
|
||||
uint8_t m0, uint8_t m1) = 0;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
@@ -138,8 +139,8 @@ inline bool nativevalue_to_se(const std::vector<std::shared_ptr<cc::physics::Tri
|
||||
se::HandleObject array(se::Object::createArrayObject(from.size() * cc::physics::TriggerEventPair::COUNT));
|
||||
for (size_t i = 0; i < from.size(); i++) {
|
||||
auto t = i * cc::physics::TriggerEventPair::COUNT;
|
||||
array->setArrayElement(static_cast<uint>(t + 0), se::Value(from[i]->shapeA));
|
||||
array->setArrayElement(static_cast<uint>(t + 1), se::Value(from[i]->shapeB));
|
||||
array->setArrayElement(static_cast<uint>(t + 0), se::Value(static_cast<double>(from[i]->shapeA)));
|
||||
array->setArrayElement(static_cast<uint>(t + 1), se::Value(static_cast<double>(from[i]->shapeB)));
|
||||
array->setArrayElement(static_cast<uint>(t + 2), se::Value(static_cast<uint8_t>(from[i]->state)));
|
||||
}
|
||||
to.setObject(array);
|
||||
@@ -148,10 +149,10 @@ inline bool nativevalue_to_se(const std::vector<std::shared_ptr<cc::physics::Tri
|
||||
|
||||
template <>
|
||||
inline bool nativevalue_to_se(const std::vector<cc::physics::ContactPoint> &from, se::Value &to, se::Object * /*ctx*/) {
|
||||
const auto contactCount = from.size();
|
||||
const auto contactCount = from.size();
|
||||
se::HandleObject array(se::Object::createArrayObject(contactCount));
|
||||
for (size_t i = 0; i < contactCount; i++) {
|
||||
auto t = i * cc::physics::ContactPoint::COUNT;
|
||||
auto t = i * cc::physics::ContactPoint::COUNT;
|
||||
uint32_t j = 0;
|
||||
array->setArrayElement(static_cast<uint>(t + j++), se::Value(from[i].position.x));
|
||||
array->setArrayElement(static_cast<uint>(t + j++), se::Value(from[i].position.y));
|
||||
@@ -175,8 +176,8 @@ inline bool nativevalue_to_se(const std::vector<std::shared_ptr<cc::physics::Con
|
||||
se::HandleObject array(se::Object::createArrayObject(from.size() * cc::physics::ContactEventPair::COUNT));
|
||||
for (size_t i = 0; i < from.size(); i++) {
|
||||
auto t = i * cc::physics::ContactEventPair::COUNT;
|
||||
array->setArrayElement(static_cast<uint>(t + 0), se::Value(from[i]->shapeA));
|
||||
array->setArrayElement(static_cast<uint>(t + 1), se::Value(from[i]->shapeB));
|
||||
array->setArrayElement(static_cast<uint>(t + 0), se::Value(static_cast<double>(from[i]->shapeA)));
|
||||
array->setArrayElement(static_cast<uint>(t + 1), se::Value(static_cast<double>(from[i]->shapeB)));
|
||||
array->setArrayElement(static_cast<uint>(t + 2), se::Value(static_cast<uint8_t>(from[i]->state)));
|
||||
array->setArrayElement(static_cast<uint>(t + 3), [&]() -> se::Value {
|
||||
auto obj = se::Value();
|
||||
@@ -191,7 +192,7 @@ inline bool nativevalue_to_se(const std::vector<std::shared_ptr<cc::physics::Con
|
||||
template <>
|
||||
inline bool nativevalue_to_se(const cc::physics::RaycastResult &from, se::Value &to, se::Object *ctx) {
|
||||
se::HandleObject obj(se::Object::createPlainObject());
|
||||
obj->setProperty("shape", se::Value(from.shape));
|
||||
obj->setProperty("shape", se::Value(static_cast<double>(from.shape)));
|
||||
obj->setProperty("distance", se::Value(from.distance));
|
||||
se::Value tmp;
|
||||
if (nativevalue_to_se(from.hitPoint, tmp, ctx)) obj->setProperty("hitPoint", tmp);
|
||||
@@ -204,14 +205,14 @@ template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, cc::physics::ConvexDesc *to, se::Object *ctx) {
|
||||
assert(from.isObject());
|
||||
se::Object *json = from.toObject();
|
||||
auto *data = static_cast<cc::physics::ConvexDesc *>(json->getPrivateData());
|
||||
auto * data = static_cast<cc::physics::ConvexDesc *>(json->getPrivateData());
|
||||
if (data) {
|
||||
*to = *data;
|
||||
return true;
|
||||
}
|
||||
|
||||
se::Value field;
|
||||
bool ok = true;
|
||||
bool ok = true;
|
||||
|
||||
json->getProperty("positionLength", &field);
|
||||
if (!field.isNullOrUndefined()) ok &= sevalue_to_native(field, &to->positionLength, ctx);
|
||||
@@ -241,13 +242,13 @@ inline bool sevalue_to_native(const se::Value &from, cc::physics::TrimeshDesc *t
|
||||
|
||||
assert(from.isObject());
|
||||
se::Object *json = from.toObject();
|
||||
auto *data = static_cast<cc::physics::TrimeshDesc *>(json->getPrivateData());
|
||||
auto * data = static_cast<cc::physics::TrimeshDesc *>(json->getPrivateData());
|
||||
if (data) {
|
||||
*to = *data;
|
||||
return true;
|
||||
}
|
||||
se::Value field;
|
||||
bool ok = true;
|
||||
bool ok = true;
|
||||
|
||||
json->getProperty("triangleLength", &field);
|
||||
if (!field.isNullOrUndefined()) ok &= sevalue_to_native(field, &(to->triangleLength), ctx);
|
||||
@@ -277,14 +278,14 @@ template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, cc::physics::HeightFieldDesc *to, se::Object *ctx) {
|
||||
assert(from.isObject());
|
||||
se::Object *json = from.toObject();
|
||||
auto *data = static_cast<cc::physics::HeightFieldDesc *>(json->getPrivateData());
|
||||
auto * data = static_cast<cc::physics::HeightFieldDesc *>(json->getPrivateData());
|
||||
if (data) {
|
||||
*to = *data;
|
||||
return true;
|
||||
}
|
||||
|
||||
se::Value field;
|
||||
bool ok = true;
|
||||
bool ok = true;
|
||||
|
||||
json->getProperty("rows", &field);
|
||||
if (!field.isNullOrUndefined()) ok &= sevalue_to_native(field, &to->rows, ctx);
|
||||
@@ -313,14 +314,14 @@ template <>
|
||||
inline bool sevalue_to_native(const se::Value &from, cc::physics::RaycastOptions *to, se::Object *ctx) {
|
||||
assert(from.isObject());
|
||||
se::Object *json = from.toObject();
|
||||
auto *data = static_cast<cc::physics::RaycastOptions *>(json->getPrivateData());
|
||||
auto * data = static_cast<cc::physics::RaycastOptions *>(json->getPrivateData());
|
||||
if (data) {
|
||||
*to = *data;
|
||||
return true;
|
||||
}
|
||||
|
||||
se::Value field;
|
||||
bool ok = true;
|
||||
bool ok = true;
|
||||
|
||||
json->getProperty("origin", &field);
|
||||
if (!field.isNullOrUndefined()) ok &= sevalue_to_native(field, &to->origin, ctx);
|
||||
|
||||
@@ -599,7 +599,7 @@ uint getPhaseID(const String &phase) {
|
||||
args.push_back(se::Value(phase));
|
||||
se::Value nrResult;
|
||||
nrPhase.toObject()->call(args, nullptr, &nrResult);
|
||||
return nrResult.toUint();
|
||||
return nrResult.toUint32();
|
||||
}
|
||||
} // namespace pipeline
|
||||
} // namespace cc
|
||||
|
||||
@@ -4,5 +4,6 @@ mkdir -p build
|
||||
cp utils/CMakeLists.header.txt build/CMakeLists.txt
|
||||
cmake -Sbuild -Bbuild -DCC_USE_GLES2=OFF -DCC_USE_VULKAN=OFF -DCC_USE_GLES3=OFF -DCC_USE_METAL=ON \
|
||||
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
|
||||
-DUSE_PHYSICS_PHYSX=ON \
|
||||
-G"Unix Makefiles"
|
||||
cp build/compile_commands.json .
|
||||
cp build/compile_commands.json .
|
||||
|
||||
@@ -20,7 +20,9 @@ cmake -Bbuild \
|
||||
-DCMAKE_CXX_FLAGS="-I$ndk_path/llvm/include/c++/v1" \
|
||||
-DCMAKE_C_FLAGS="-I$ndk_path/llvm/include/c++/v1" \
|
||||
-DCMAKE_MAKE_PROGRAM="$ndk_path/build-tools/cmake/bin/ninja" \
|
||||
-GNinja
|
||||
-GNinja \
|
||||
-DUSE_SOCKET=ON \
|
||||
-DUSE_WEBSOCKET_SERVER=ON
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user