Skip to content

Commit

Permalink
feat(core): fix lint warning and fix iOS demo bug
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed Nov 24, 2021
1 parent 49c9c4f commit 2bec712
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 156 deletions.
11 changes: 4 additions & 7 deletions core/include/core/modules/timer_module.h
Expand Up @@ -56,14 +56,11 @@ class TimerModule : public ModuleBase {
void Cancel(TaskId task_id, const std::shared_ptr<Scope>& scope);

struct TaskEntry {
TaskEntry(std::shared_ptr<Ctx> context,
std::weak_ptr<JavaScriptTask> task) {
task_ = std::move(task);
context_ = context;
}
TaskEntry(std::shared_ptr<CtxValue> func,
std::weak_ptr<JavaScriptTask> task): func(func), task(task) {}

std::weak_ptr<JavaScriptTask> task_;
std::shared_ptr<Ctx> context_;
std::shared_ptr<CtxValue> func;
std::weak_ptr<JavaScriptTask> task;
};

std::unordered_map<TaskId, std::shared_ptr<TaskEntry>> task_map_;
Expand Down
50 changes: 26 additions & 24 deletions core/include/core/napi/js_native_api_types.h
Expand Up @@ -88,17 +88,17 @@ class Ctx {
virtual bool SetGlobalStrVar(const unicode_string_view& name,
const unicode_string_view& str) = 0;
virtual bool SetGlobalObjVar(const unicode_string_view& name,
std::shared_ptr<CtxValue> obj,
PropertyAttribute attr = None) = 0;
const std::shared_ptr<CtxValue>& obj,
const PropertyAttribute& attr) = 0;
virtual std::shared_ptr<CtxValue> GetGlobalStrVar(
const unicode_string_view& name) = 0;
virtual std::shared_ptr<CtxValue> GetGlobalObjVar(
const unicode_string_view& name) = 0;
virtual std::shared_ptr<CtxValue> GetProperty(
const std::shared_ptr<CtxValue> object,
const std::shared_ptr<CtxValue>& object,
const unicode_string_view& name) = 0;

virtual void RegisterGlobalModule(std::shared_ptr<Scope> scope,
virtual void RegisterGlobalModule(const std::shared_ptr<Scope>& scope,
const ModuleClassMap& modules) = 0;
virtual void RegisterNativeBinding(const unicode_string_view& name,
hippy::base::RegisterFunction fn,
Expand All @@ -112,6 +112,7 @@ class Ctx {
virtual std::shared_ptr<CtxValue> CreateNull() = 0;
virtual std::shared_ptr<CtxValue> CreateObject(
const unicode_string_view& json) = 0;
virtual std::shared_ptr<CtxValue> CreateMap(size_t count, std::shared_ptr<CtxValue>* value) = 0;
virtual std::shared_ptr<CtxValue> CreateArray(
size_t count,
std::shared_ptr<CtxValue> value[]) = 0;
Expand All @@ -120,41 +121,42 @@ class Ctx {

// Get From Value
virtual std::shared_ptr<CtxValue> CallFunction(
std::shared_ptr<CtxValue> function,
size_t argument_count = 0,
const std::shared_ptr<CtxValue> arguments[] = nullptr) = 0;
const std::shared_ptr<CtxValue>& function,
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[]) = 0;

virtual bool GetValueNumber(std::shared_ptr<CtxValue> value,
virtual bool GetValueNumber(const std::shared_ptr<CtxValue>& value,
double* result) = 0;
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value,
virtual bool GetValueNumber(const std::shared_ptr<CtxValue>& value,
int32_t* result) = 0;
virtual bool GetValueBoolean(std::shared_ptr<CtxValue> value,
virtual bool GetValueBoolean(const std::shared_ptr<CtxValue>& value,
bool* result) = 0;
virtual bool GetValueString(std::shared_ptr<CtxValue> value,
virtual bool GetValueString(const std::shared_ptr<CtxValue>& value,
unicode_string_view* result) = 0;
virtual bool GetValueJson(std::shared_ptr<CtxValue> value,
virtual bool GetValueJson(const std::shared_ptr<CtxValue>& value,
unicode_string_view* result) = 0;
virtual bool IsNullOrUndefined(std::shared_ptr<CtxValue>) = 0;
virtual bool IsNullOrUndefined(const std::shared_ptr<CtxValue>& value) = 0;

virtual bool IsMap(const std::shared_ptr<CtxValue>& value) = 0;

// Array Helpers

virtual bool IsArray(std::shared_ptr<CtxValue> value) = 0;
virtual uint32_t GetArrayLength(std::shared_ptr<CtxValue> value) = 0;
virtual std::shared_ptr<CtxValue> CopyArrayElement(std::shared_ptr<CtxValue>,
virtual bool IsArray(const std::shared_ptr<CtxValue>& value) = 0;
virtual uint32_t GetArrayLength(const std::shared_ptr<CtxValue>& value) = 0;
virtual std::shared_ptr<CtxValue> CopyArrayElement(const std::shared_ptr<CtxValue>& value,
uint32_t index) = 0;

// Object Helpers

virtual bool HasNamedProperty(std::shared_ptr<CtxValue> value,
virtual bool HasNamedProperty(const std::shared_ptr<CtxValue>& value,
const unicode_string_view& name) = 0;
virtual std::shared_ptr<CtxValue> CopyNamedProperty(
std::shared_ptr<CtxValue> value,
const std::shared_ptr<CtxValue>& value,
const unicode_string_view& name) = 0;
// Function Helpers

virtual bool IsFunction(std::shared_ptr<CtxValue> value) = 0;
virtual bool IsFunction(const std::shared_ptr<CtxValue>& value) = 0;
virtual unicode_string_view CopyFunctionName(
std::shared_ptr<CtxValue> value) = 0;
const std::shared_ptr<CtxValue>& value) = 0;

virtual std::shared_ptr<CtxValue> RunScript(
const unicode_string_view& data,
Expand All @@ -164,12 +166,12 @@ class Ctx {
bool is_copy = true) = 0;
virtual std::shared_ptr<CtxValue> GetJsFn(
const unicode_string_view& name) = 0;
virtual bool ThrowExceptionToJS(std::shared_ptr<CtxValue> exception) = 0;
virtual bool ThrowExceptionToJS(const std::shared_ptr<CtxValue>& exception) = 0;

virtual std::shared_ptr<JSValueWrapper> ToJsValueWrapper(
std::shared_ptr<CtxValue> value) = 0;
const std::shared_ptr<CtxValue>& value) = 0;
virtual std::shared_ptr<CtxValue> CreateCtxValue(
std::shared_ptr<JSValueWrapper> wrapper) = 0;
const std::shared_ptr<JSValueWrapper>& wrapper) = 0;
};

struct VMInitParam {};
Expand Down
105 changes: 56 additions & 49 deletions core/include/core/napi/jsc/js_native_api_jsc.h
Expand Up @@ -47,7 +47,7 @@ const char16_t kStackStr[] = u"stack";

class JSCVM : public VM {
public:
JSCVM() { vm_ = JSContextGroupCreate(); }
JSCVM(): VM(nullptr) { vm_ = JSContextGroupCreate(); }

~JSCVM() {
JSContextGroupRelease(vm_);
Expand Down Expand Up @@ -96,93 +96,100 @@ class JSCCtx : public Ctx {
inline void SetExceptionHandled(bool is_exception_handled) {
is_exception_handled_ = is_exception_handled;
}
virtual bool RegisterGlobalInJs();
virtual bool RegisterGlobalInJs() override;
virtual bool SetGlobalJsonVar(const unicode_string_view& name,
const unicode_string_view& json);
const unicode_string_view& json) override;
virtual bool SetGlobalStrVar(const unicode_string_view& name,
const unicode_string_view& str);
const unicode_string_view& str) override;
virtual bool SetGlobalObjVar(const unicode_string_view& name,
std::shared_ptr<CtxValue> obj,
PropertyAttribute attr = None);
const std::shared_ptr<CtxValue>& obj,
const PropertyAttribute& attr = None) override;
virtual std::shared_ptr<CtxValue> GetGlobalStrVar(
const unicode_string_view& name);
const unicode_string_view& name) override;
virtual std::shared_ptr<CtxValue> GetGlobalObjVar(
const unicode_string_view& name);
const unicode_string_view& name) override;
virtual std::shared_ptr<CtxValue> GetProperty(
const std::shared_ptr<CtxValue> object,
const unicode_string_view& name);
const std::shared_ptr<CtxValue>& object,
const unicode_string_view& name) override;

virtual void RegisterGlobalModule(std::shared_ptr<Scope> scope,
const ModuleClassMap& modules);
virtual void RegisterGlobalModule(const std::shared_ptr<Scope>& scope,
const ModuleClassMap& modules) override;
virtual void RegisterNativeBinding(const unicode_string_view& name,
hippy::base::RegisterFunction fn,
void* data);
void* data) override;

virtual std::shared_ptr<CtxValue> CreateNumber(double number);
virtual std::shared_ptr<CtxValue> CreateBoolean(bool b);
virtual std::shared_ptr<CtxValue> CreateNumber(double number) override;
virtual std::shared_ptr<CtxValue> CreateBoolean(bool b) override;
virtual std::shared_ptr<CtxValue> CreateString(
const unicode_string_view& string);
virtual std::shared_ptr<CtxValue> CreateUndefined();
virtual std::shared_ptr<CtxValue> CreateNull();
const unicode_string_view& string) override;
virtual std::shared_ptr<CtxValue> CreateUndefined() override;
virtual std::shared_ptr<CtxValue> CreateNull() override;
virtual std::shared_ptr<CtxValue> CreateObject(
const unicode_string_view& json);
const unicode_string_view& json) override;
virtual std::shared_ptr<CtxValue> CreateMap(size_t count,
std::shared_ptr<CtxValue>* value) override {
TDF_BASE_NOTIMPLEMENTED();
return nullptr;
};
virtual std::shared_ptr<CtxValue> CreateArray(
size_t count,
std::shared_ptr<CtxValue> value[]);
std::shared_ptr<CtxValue> value[]) override;
virtual std::shared_ptr<CtxValue> CreateJsError(
const unicode_string_view& msg);
const unicode_string_view& msg) override;

// Get From Value
virtual std::shared_ptr<CtxValue> CallFunction(
std::shared_ptr<CtxValue> function,
const std::shared_ptr<CtxValue>& function,
size_t argument_count = 0,
const std::shared_ptr<CtxValue> argumets[] = nullptr);

virtual bool GetValueNumber(std::shared_ptr<CtxValue> value, double* result);
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value, int32_t* result);
virtual bool GetValueBoolean(std::shared_ptr<CtxValue> value, bool* result);
virtual bool GetValueString(std::shared_ptr<CtxValue> value,
unicode_string_view* result);
virtual bool GetValueJson(std::shared_ptr<CtxValue> value,
unicode_string_view* result);

const std::shared_ptr<CtxValue> argumets[] = nullptr) override;

virtual bool GetValueNumber(const std::shared_ptr<CtxValue>& value, double* result) override;
virtual bool GetValueNumber(const std::shared_ptr<CtxValue>& value, int32_t* result) override;
virtual bool GetValueBoolean(const std::shared_ptr<CtxValue>& value, bool* result) override;
virtual bool GetValueString(const std::shared_ptr<CtxValue>& value,
unicode_string_view* result) override;
virtual bool GetValueJson(const std::shared_ptr<CtxValue>& value,
unicode_string_view* result) override;
virtual bool IsMap(const std::shared_ptr<CtxValue>& value) override {
TDF_BASE_NOTIMPLEMENTED();
return false;
};
// Null Helpers
virtual bool IsNullOrUndefined(std::shared_ptr<CtxValue>);
virtual bool IsNullOrUndefined(const std::shared_ptr<CtxValue>& value) override;

// Array Helpers

virtual bool IsArray(std::shared_ptr<CtxValue> value);
virtual uint32_t GetArrayLength(std::shared_ptr<CtxValue> value);
virtual std::shared_ptr<CtxValue> CopyArrayElement(std::shared_ptr<CtxValue>,
uint32_t index);
virtual bool IsArray(const std::shared_ptr<CtxValue>& value) override;
virtual uint32_t GetArrayLength(const std::shared_ptr<CtxValue>& value) override;
virtual std::shared_ptr<CtxValue> CopyArrayElement(const std::shared_ptr<CtxValue>& value, uint32_t index) override;

// Object Helpers

virtual bool HasNamedProperty(std::shared_ptr<CtxValue> value,
const unicode_string_view& name);
virtual bool HasNamedProperty(const std::shared_ptr<CtxValue>& value,
const unicode_string_view& name) override;
virtual std::shared_ptr<CtxValue> CopyNamedProperty(
std::shared_ptr<CtxValue> value,
const unicode_string_view& name);
const std::shared_ptr<CtxValue>& value,
const unicode_string_view& name) override;
// Function Helpers

virtual bool IsFunction(std::shared_ptr<CtxValue> value);
virtual unicode_string_view CopyFunctionName(std::shared_ptr<CtxValue> value);
virtual bool IsFunction(const std::shared_ptr<CtxValue>& value) override;
virtual unicode_string_view CopyFunctionName(const std::shared_ptr<CtxValue>& value) override;

virtual std::shared_ptr<CtxValue> RunScript(
const unicode_string_view& data,
const unicode_string_view& file_name,
bool is_use_code_cache = false,
unicode_string_view* cache = nullptr,
bool is_copy = true);
virtual std::shared_ptr<CtxValue> GetJsFn(const unicode_string_view& name);
virtual bool ThrowExceptionToJS(std::shared_ptr<CtxValue> exception);
bool is_copy = true) override;
virtual std::shared_ptr<CtxValue> GetJsFn(const unicode_string_view& name) override;
virtual bool ThrowExceptionToJS(const std::shared_ptr<CtxValue>& exception) override;

virtual std::shared_ptr<JSValueWrapper> ToJsValueWrapper(
std::shared_ptr<CtxValue> value);
const std::shared_ptr<CtxValue>& value) override;
virtual std::shared_ptr<CtxValue> CreateCtxValue(
std::shared_ptr<JSValueWrapper> wrapper);
const std::shared_ptr<JSValueWrapper>& wrapper) override;

unicode_string_view GetExceptionMsg(std::shared_ptr<CtxValue> exception);
unicode_string_view GetExceptionMsg(const std::shared_ptr<CtxValue>& exception);
JSStringRef CreateJSCString(const unicode_string_view& str_view);

JSGlobalContextRef context_;
Expand Down
42 changes: 21 additions & 21 deletions core/include/core/napi/v8/js_native_api_v8.h
Expand Up @@ -134,17 +134,17 @@ class V8Ctx : public Ctx {
virtual bool SetGlobalStrVar(const unicode_string_view& name,
const unicode_string_view& str);
virtual bool SetGlobalObjVar(const unicode_string_view& name,
std::shared_ptr<CtxValue> obj,
PropertyAttribute attr = None);
const std::shared_ptr<CtxValue>& obj,
const PropertyAttribute& attr = None);
virtual std::shared_ptr<CtxValue> GetGlobalStrVar(
const unicode_string_view& name);
virtual std::shared_ptr<CtxValue> GetGlobalObjVar(
const unicode_string_view& name);
virtual std::shared_ptr<CtxValue> GetProperty(
const std::shared_ptr<CtxValue> object,
const std::shared_ptr<CtxValue>& object,
const unicode_string_view& name);

virtual void RegisterGlobalModule(std::shared_ptr<Scope> scope,
virtual void RegisterGlobalModule(const std::shared_ptr<Scope>& scope,
const ModuleClassMap& modules);
virtual void RegisterNativeBinding(const unicode_string_view& name,
hippy::base::RegisterFunction fn,
Expand All @@ -169,27 +169,27 @@ class V8Ctx : public Ctx {

// Get From Value
virtual std::shared_ptr<CtxValue> CallFunction(
std::shared_ptr<CtxValue> function,
const std::shared_ptr<CtxValue>& function,
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[] = nullptr);

virtual bool GetValueNumber(std::shared_ptr<CtxValue> value, double* result);
virtual bool GetValueNumber(std::shared_ptr<CtxValue> value, int32_t* result);
virtual bool GetValueBoolean(std::shared_ptr<CtxValue> value, bool* result);
virtual bool GetValueString(std::shared_ptr<CtxValue> value,
virtual bool GetValueNumber(const std::shared_ptr<CtxValue>& value, double* result);
virtual bool GetValueNumber(const std::shared_ptr<CtxValue>& value, int32_t* result);
virtual bool GetValueBoolean(const std::shared_ptr<CtxValue>& value, bool* result);
virtual bool GetValueString(const std::shared_ptr<CtxValue>& value,
unicode_string_view* result);
virtual bool GetValueJson(std::shared_ptr<CtxValue> value,
virtual bool GetValueJson(const std::shared_ptr<CtxValue>& value,
unicode_string_view* result);

virtual bool IsMap(const std::shared_ptr<CtxValue>& value);

virtual bool IsNullOrUndefined(std::shared_ptr<CtxValue> value);
virtual bool IsNullOrUndefined(const std::shared_ptr<CtxValue>& value);

// Array Helpers

virtual bool IsArray(std::shared_ptr<CtxValue> value);
virtual uint32_t GetArrayLength(std::shared_ptr<CtxValue> value);
virtual std::shared_ptr<CtxValue> CopyArrayElement(std::shared_ptr<CtxValue>,
virtual bool IsArray(const std::shared_ptr<CtxValue>& value);
virtual uint32_t GetArrayLength(const std::shared_ptr<CtxValue>& value);
virtual std::shared_ptr<CtxValue> CopyArrayElement(const std::shared_ptr<CtxValue>& value,
uint32_t index);

// Map Helpers
Expand All @@ -199,15 +199,15 @@ class V8Ctx : public Ctx {

// Object Helpers

virtual bool HasNamedProperty(std::shared_ptr<CtxValue> value,
virtual bool HasNamedProperty(const std::shared_ptr<CtxValue>& value,
const unicode_string_view& utf8name);
virtual std::shared_ptr<CtxValue> CopyNamedProperty(
std::shared_ptr<CtxValue> value,
const std::shared_ptr<CtxValue>& value,
const unicode_string_view& utf8name);
// Function Helpers

virtual bool IsFunction(std::shared_ptr<CtxValue> value);
virtual unicode_string_view CopyFunctionName(std::shared_ptr<CtxValue> value);
virtual bool IsFunction(const std::shared_ptr<CtxValue>& value);
virtual unicode_string_view CopyFunctionName(const std::shared_ptr<CtxValue>& value);

virtual std::shared_ptr<CtxValue> RunScript(
const unicode_string_view& data,
Expand All @@ -217,12 +217,12 @@ class V8Ctx : public Ctx {
bool is_copy = true);

virtual std::shared_ptr<CtxValue> GetJsFn(const unicode_string_view& name);
virtual bool ThrowExceptionToJS(std::shared_ptr<CtxValue> exception);
virtual bool ThrowExceptionToJS(const std::shared_ptr<CtxValue>& exception);

virtual std::shared_ptr<JSValueWrapper> ToJsValueWrapper(
std::shared_ptr<CtxValue> value);
const std::shared_ptr<CtxValue>& value);
virtual std::shared_ptr<CtxValue> CreateCtxValue(
std::shared_ptr<JSValueWrapper> wrapper);
const std::shared_ptr<JSValueWrapper>& wrapper);

unicode_string_view ToStringView(v8::Local<v8::String> str) const;
unicode_string_view GetMsgDesc(v8::Local<v8::Message> message);
Expand Down
3 changes: 2 additions & 1 deletion core/src/modules/contextify_module.cc
Expand Up @@ -162,7 +162,8 @@ void ContextifyModule::LoadUntrustedContent(const CallbackInfo& info) {
try_catch->SetVerbose(true);
unicode_string_view view_code(move_code);
scope->RunJS(view_code, file_name);
ctx->SetGlobalObjVar("__HIPPYCURDIR__", last_dir_str_obj);
ctx->SetGlobalObjVar("__HIPPYCURDIR__", last_dir_str_obj,
hippy::napi::PropertyAttribute::None);
unicode_string_view view_last_dir_str;
ctx->GetValueString(last_dir_str_obj, &view_last_dir_str);
TDF_BASE_DLOG(INFO)
Expand Down

0 comments on commit 2bec712

Please sign in to comment.