Skip to content

symbol #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/quickjs-msvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,13 @@ JSValue JS_SetDelete(JSContext *ctx, JSValueConst this_val,

void JS_SetClear(JSContext *ctx, JSValueConst this_val);
JSValue JS_DupModule(JSContext *ctx, JSModuleDef* v);

/*-------end fuctions for v8 api---------*/
JSValue JS_GET_MODULE_NS(JSContext *ctx, JSModuleDef* v);
int JS_ReleaseLoadedModule(JSContext *ctx, const char* path);

JSValue JS_NewSymbolByAtom(JSContext *ctx, JSAtom descr, int atom_type);
JSAtom JS_SymbolToAtom(JSContext *ctx, JSValue val);
/*-------end fuctions for v8 api---------*/

#undef js_unlikely
#undef js_force_inline

Expand Down
18 changes: 17 additions & 1 deletion include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,21 @@ class V8_EXPORT String : public Name {
private:
};

/**
* A JavaScript symbol (ECMA-262 edition 6)
*/
class V8_EXPORT Symbol : public Name {
public:
static Local<Symbol> New(
Isolate* isolate,
Local<String> description = Local<String>()
);

V8_INLINE static Symbol* Cast(Value* data) {
return static_cast<Symbol*>(data);
}
};

class V8_EXPORT Function : public Object {
public:
V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
Expand Down Expand Up @@ -1338,7 +1353,8 @@ class V8_EXPORT Template : public Data {
Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
PropertyAttribute attribute = None);

std::map<std::string, Local<Data>> fields_;
// std::map<std::string, Local<Data>> fields_;
std::map<JSAtom, Local<Data>> fieldsByAtom_;

class AccessorPropertyInfo {
public:
Expand Down
15 changes: 11 additions & 4 deletions quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -54158,9 +54158,6 @@ JSValue JS_DupModule(JSContext *ctx, JSModuleDef* v)
{
return JS_DupValue(ctx, JS_MKPTR(JS_TAG_MODULE, v));
}

/*-------end fuctions for v8 api---------*/

JSValue JS_GET_MODULE_NS(JSContext *ctx, JSModuleDef* v)
{
return js_get_module_ns(ctx, v);
Expand All @@ -54186,4 +54183,14 @@ int JS_ReleaseLoadedModule(JSContext *ctx, const char* path)
}
JS_FreeAtom(ctx, name);
return 0;
}
}
JSValue JS_NewSymbolByAtom(JSContext *ctx, JSAtom descr, int atom_type)
{
return JS_NewSymbolFromAtom(ctx, descr, atom_type);
}

JSAtom JS_SymbolToAtom(JSContext *ctx, JSValue val)
{
return js_symbol_to_atom(ctx, val);
}
/*-------end fuctions for v8 api---------*/
42 changes: 35 additions & 7 deletions src/v8-impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ Isolate* Promise::GetIsolate() {
return Isolate::current_;
}

Local<Symbol> Symbol::New(Isolate* isolate, Local<String> description) {
auto symbol = isolate->Alloc<Symbol>();
auto context = isolate->current_context_->context_;
auto atom = JS_NewAtom(context, *String::Utf8Value(isolate, description));
symbol->value_ = JS_NewSymbolByAtom(
isolate->current_context_->context_,
atom,
2
);
JS_FreeAtom(context, atom);
return Local<Symbol>(symbol);
}

void V8FinalizerWrap(JSRuntime *rt, JSValue val) {
Isolate* isolate = (Isolate*)JS_GetRuntimeOpaque(rt);
Isolate::Scope Isolatescope(isolate);
Expand Down Expand Up @@ -428,7 +441,6 @@ int String::Utf8Length(Isolate* isolate) const {
int String::WriteUtf8(Isolate* isolate, char* buffer) const {
size_t len;
const char* p = JS_ToCStringLen(isolate->current_context_->context_, &len, value_);

memcpy(buffer, p, len);

JS_FreeCString(isolate->current_context_->context_, p);
Expand Down Expand Up @@ -804,13 +816,22 @@ MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc, Local
}

void Template::Set(Isolate* isolate, const char* name, Local<Data> value) {
fields_[name] = value;
// fields_[name] = value;
fieldsByAtom_[JS_NewAtom(isolate->current_context_->context_, name)] = value;
}

void Template::Set(Local<Name> name, Local<Data> value,
PropertyAttribute attributes) {
Isolate* isolate = Isolate::current_;
Set(isolate, *String::Utf8Value(Isolate::current_, name), value);

if (name->IsSymbol())
{
fieldsByAtom_[JS_SymbolToAtom(isolate->current_context_->context_, Symbol::Cast(*name)->value_)] = value;
}
else
{
Set(isolate, *String::Utf8Value(Isolate::current_, name), value);
}
}

void Template::SetAccessorProperty(Local<Name> name,
Expand All @@ -822,13 +843,20 @@ void Template::SetAccessorProperty(Local<Name> name,
}

void Template::InitPropertys(Local<Context> context, JSValue obj) {
for(auto it : fields_) {
JSAtom atom = JS_NewAtom(context->context_, it.first.data());
// for(auto it : fields_) {
// JSAtom atom = JS_NewAtom(context->context_, it.first.data());
// Local<FunctionTemplate> funcTpl = Local<FunctionTemplate>::Cast(it.second);
// Local<Function> lfunc = funcTpl->GetFunction(context).ToLocalChecked();
// context->GetIsolate()->Escape(*lfunc);
// JS_DefinePropertyValue(context->context_, obj, atom, lfunc->value_, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_WRITABLE);
// JS_FreeAtom(context->context_, atom);
// }
for(auto it : fieldsByAtom_) {
JSAtom atom = it.first;
Local<FunctionTemplate> funcTpl = Local<FunctionTemplate>::Cast(it.second);
Local<Function> lfunc = funcTpl->GetFunction(context).ToLocalChecked();
context->GetIsolate()->Escape(*lfunc);
JS_DefinePropertyValue(context->context_, obj, atom, lfunc->value_, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_WRITABLE);
JS_FreeAtom(context->context_, atom);
}

for (auto it : accessor_property_infos_) {
Expand Down Expand Up @@ -1034,7 +1062,7 @@ MaybeLocal<Function> FunctionTemplate::GetFunction(Local<Context> context) {
JS_DupValueRT(isolate_->runtime_, ret->value_);
return MaybeLocal<Function>(Local<Function>(ret));
}
cfunction_data_.is_construtor_ = !prototype_template_.IsEmpty() || !instance_template_.IsEmpty() || fields_.size() > 0 || accessor_property_infos_.size() > 0 || !parent_.IsEmpty();
cfunction_data_.is_construtor_ = !prototype_template_.IsEmpty() || !instance_template_.IsEmpty() || fieldsByAtom_.size() > 0 || accessor_property_infos_.size() > 0 || !parent_.IsEmpty();
cfunction_data_.internal_field_count_ = instance_template_.IsEmpty() ? 0 : instance_template_->internal_field_count_;

JSValue func_data[4];
Expand Down