Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
disable log when debugger is off
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzilin committed Feb 8, 2022
1 parent 6f7ee4d commit fb47829
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 41 deletions.
6 changes: 4 additions & 2 deletions es/enter_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ void DeclarationBindingInstantiation(
Handle<Error>& e, ExecutionContext* context, AST* code, CodeType code_type,
Handle<FunctionObject> f = Handle<FunctionObject>(), std::vector<Handle<JSValue>> args = {}
) {
log::PrintSource("enter DeclarationBindingInstantiation");
if (log::Debugger::On())
log::PrintSource("enter DeclarationBindingInstantiation");
Handle<EnvironmentRecord> env = context->variable_env().val()->env_rec(); // 1
bool configurable_bindings = false;
ProgramOrFunctionBody* body = static_cast<ProgramOrFunctionBody*>(code);
Expand Down Expand Up @@ -320,7 +321,8 @@ void EnterEvalCode(Handle<Error>& e, AST* ast) {

// 15.1.2.1 eval(X)
Handle<JSValue> GlobalObject::eval(Handle<Error>& e, Handle<JSValue> this_arg, std::vector<Handle<JSValue>> vals) {
log::PrintSource("enter GlobalObject::eval");
if (log::Debugger::On())
log::PrintSource("enter GlobalObject::eval");
if (vals.size() == 0)
return Undefined::Instance();
if (!vals[0].val()->IsString())
Expand Down
6 changes: 4 additions & 2 deletions es/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ Completion EvalProgram(AST* ast) {
}

Completion EvalStatement(AST* ast) {
log::PrintSource("EvalStatement ", ast->source().substr(0, 50));
if (log::Debugger::On())
log::PrintSource("EvalStatement ", ast->source().substr(0, 50));
Completion C(Completion::NORMAL, Handle<JSValue>(), u"");
JSValue* val = nullptr;
{
Expand Down Expand Up @@ -739,7 +740,8 @@ Handle<JSValue> EvalExpression(Handle<Error>& e, AST* ast) {
val = EvalFunction(e, ast);
break;
default:
log::PrintSource("ast: ", ast->source(), "type: " + std::to_string(ast->type()));
if (log::Debugger::On())
log::PrintSource("ast: ", ast->source(), "type: " + std::to_string(ast->type()));
assert(false);
}
if (!e.val()->IsOk()) return Handle<JSValue>();
Expand Down
25 changes: 16 additions & 9 deletions es/impl/call-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ Handle<JSValue> Call__Function(
Handle<Error>& e, Handle<FunctionObject> O, Handle<JSValue> this_arg, std::vector<Handle<JSValue>> arguments
) {
ProgramOrFunctionBody* code = O.val()->Code();
log::PrintSource("enter FunctionObject::Call ", code->source().substr(0, 100));
if (log::Debugger::On())
log::PrintSource("enter FunctionObject::Call ", code->source().substr(0, 100));
EnterFunctionCode(e, O, code, this_arg, arguments, O.val()->strict());
if (!e.val()->IsOk()) return Handle<JSValue>();

Expand All @@ -85,26 +86,31 @@ Handle<JSValue> Call__Function(
result = EvalProgram(code);
}
Runtime::Global()->PopContext(); // 3

log::PrintSource("exit FunctionObject::Call", code->source().substr(0, 100));
if (log::Debugger::On())
log::PrintSource("exit FunctionObject::Call", code->source().substr(0, 100));
switch (result.type()) {
case Completion::RETURN:
log::PrintSource("exit FunctionObject::Call RETURN");
if (log::Debugger::On())
log::PrintSource("exit FunctionObject::Call RETURN");
return result.value();
case Completion::THROW: {
log::PrintSource("exit FunctionObject::Call THROW");
if (log::Debugger::On())
log::PrintSource("exit FunctionObject::Call THROW");
Handle<JSValue> throw_value = result.value();
if (throw_value.val()->IsErrorObject()) {
e = static_cast<Handle<ErrorObject>>(throw_value).val()->e();
log::PrintSource("message: " + e.ToString());
if (log::Debugger::On())
log::PrintSource("message: " + e.ToString());
return Handle<JSValue>();
}
log::PrintSource("message: " + throw_value.ToString());
if (log::Debugger::On())
log::PrintSource("message: " + throw_value.ToString());
e = Error::NativeError(throw_value);
return Handle<JSValue>();
}
default:
log::PrintSource("exit FunctionObject::Call NORMAL");
if (log::Debugger::On())
log::PrintSource("exit FunctionObject::Call NORMAL");
assert(result.type() == Completion::NORMAL);
return Undefined::Instance();
}
Expand All @@ -113,7 +119,8 @@ Handle<JSValue> Call__Function(
Handle<JSValue> Call__BindFunction(
Handle<Error>& e, Handle<BindFunctionObject> O, Handle<JSValue> this_arg, std::vector<Handle<JSValue>> extra_args
) {
log::PrintSource("enter BindFunctionObject::Call");
if (log::Debugger::On())
log::PrintSource("enter BindFunctionObject::Call");
Handle<FixedArray<JSValue>> bound_args = O.val()->BoundArgs();
Handle<JSObject> target_function = O.val()->TargetFunction();

Expand Down
6 changes: 4 additions & 2 deletions es/impl/construct-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Handle<JSObject> Construct(Handle<Error>& e, Handle<JSObject> O, std::vector<Han
Handle<JSObject> Construct__Function(
Handle<Error>& e, Handle<FunctionObject> O, std::vector<Handle<JSValue>> arguments
) {
log::PrintSource("enter FunctionObject::Construct");
if (log::Debugger::On())
log::PrintSource("enter FunctionObject::Construct");
// NOTE(zhuzilin) I'm not sure if the object type should be OBJ_OBJECT or OBJ_OTHER...
Handle<JSObject> obj = JSObject::New(JSObject::OBJ_OBJECT, u"Object", true, Handle<JSValue>(), false, false, nullptr, 0);
Handle<JSValue> proto = Get(e, O, String::Prototype());
Expand Down Expand Up @@ -134,7 +135,8 @@ Handle<JSObject> Construct__ErrorConstructor(
Handle<JSObject> Construct__FunctionConstructor(
Handle<Error>& e, Handle<FunctionConstructor> O, std::vector<Handle<JSValue>> arguments
) {
log::PrintSource("enter FunctionConstructor::Construct");
if (log::Debugger::On())
log::PrintSource("enter FunctionConstructor::Construct");
size_t arg_count = arguments.size();
std::u16string P = u"";
std::u16string body = u"";
Expand Down
18 changes: 12 additions & 6 deletions es/impl/environment_record-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void SetMutableBinding(
void SetMutableBinding__Declarative(
Handle<Error>& e, Handle<DeclarativeEnvironmentRecord> env_rec, Handle<String> N, Handle<JSValue> V, bool S
) {
log::PrintSource("enter SetMutableBinding__Declarative ", N.val()->data(), " to " + V.ToString());
if (log::Debugger::On())
log::PrintSource("enter SetMutableBinding__Declarative ", N.val()->data(), " to " + V.ToString());
assert(V.val()->IsLanguageType());
assert(HasBinding(env_rec, N));
// NOTE(zhuzilin) If we do note b = bindings_[N] and change b.value,
Expand All @@ -88,7 +89,8 @@ void SetMutableBinding__Declarative(
void SetMutableBinding__Object(
Handle<Error>& e, Handle<ObjectEnvironmentRecord> env_rec, Handle<String> N, Handle<JSValue> V, bool S
) {
log::PrintSource("enter SetMutableBinding__Object " + N.ToString() + " to " + V.ToString());
if (log::Debugger::On())
log::PrintSource("enter SetMutableBinding__Object " + N.ToString() + " to " + V.ToString());
assert(V.val()->IsLanguageType());
Put(e, env_rec.val()->bindings(), N, V, S);
}
Expand All @@ -108,27 +110,31 @@ Handle<JSValue> GetBindingValue(
Handle<JSValue> GetBindingValue__Declarative(
Handle<Error>& e, Handle<DeclarativeEnvironmentRecord> env_rec, Handle<String> N, bool S
) {
log::PrintSource("enter GetBindingValue__Declarative " + N.ToString());
if (log::Debugger::On())
log::PrintSource("enter GetBindingValue__Declarative " + N.ToString());
assert(HasBinding(env_rec, N));
DeclarativeEnvironmentRecord::Binding* b = env_rec.val()->bindings()->GetRaw(N);
if (b->value().val()->IsUndefined() && !b->is_mutable()) {
if (S) {
e = Error::ReferenceError(N.val()->data() + u" is not defined");
return Handle<JSValue>();
} else {
log::PrintSource("GetBindingValue ", N.val()->data(), " undefined");
if (log::Debugger::On())
log::PrintSource("GetBindingValue ", N.val()->data(), " undefined");
return Undefined::Instance();
}
}
log::PrintSource("GetBindingValue ", N.val()->data(), " " + b->value().ToString());
if (log::Debugger::On())
log::PrintSource("GetBindingValue ", N.val()->data(), " " + b->value().ToString());
return b->value();
}

// 10.2.1.2.4 GetBindingValue(N,S)
Handle<JSValue> GetBindingValue__Object(
Handle<Error>& e, Handle<ObjectEnvironmentRecord> env_rec, Handle<String> N, bool S
) {
log::PrintSource("enter GetBindingValue__Object " + N.ToString());
if (log::Debugger::On())
log::PrintSource("enter GetBindingValue__Object " + N.ToString());
bool value = HasBinding(env_rec, N);
if (!value) {
if (S) {
Expand Down
36 changes: 24 additions & 12 deletions es/impl/object-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ Handle<JSValue> GetProperty(Handle<JSObject> O, Handle<String> P) {

// [[Get]]
Handle<JSValue> Get(Handle<Error>& e, Handle<JSObject> O, Handle<String> P) {
log::PrintSource("enter Get " + P.ToString() + " from " + std::to_string(O.val()->obj_type()));
if (log::Debugger::On())
log::PrintSource("enter Get " + P.ToString() + " from " + std::to_string(O.val()->obj_type()));
if (O.val()->IsFunctionObject()) {
return Get__Function(e, static_cast<Handle<FunctionObject>>(O), P);
} else if (O.val()->IsArgumentsObject()) {
Expand Down Expand Up @@ -196,7 +197,8 @@ bool CanPut(Handle<JSObject> O, Handle<String> P) {
// [[Put]]
// 8.12.5 [[Put]] ( P, V, Throw )
void Put(Handle<Error>& e, Handle<JSObject> O, Handle<String> P, Handle<JSValue> V, bool throw_flag) {
log::PrintSource("enter Put " + P.ToString() + " to " + O.ToString() + " with value " + V.ToString());
if (log::Debugger::On())
log::PrintSource("enter Put " + P.ToString() + " to " + O.ToString() + " with value " + V.ToString());
assert(V.val()->IsLanguageType());
if (!CanPut(O, P)) { // 1
if (throw_flag) { // 1.a
Expand All @@ -210,7 +212,8 @@ void Put(Handle<Error>& e, Handle<JSObject> O, Handle<String> P, Handle<JSValue>
if (own_desc.val()->IsDataDescriptor()) { // 3
Handle<PropertyDescriptor> value_desc = PropertyDescriptor::New();
value_desc.val()->SetValue(V);
log::PrintSource("Overwrite the old desc with " + value_desc.ToString());
if (log::Debugger::On())
log::PrintSource("Overwrite the old desc with " + value_desc.ToString());
DefineOwnProperty(e, O, P, value_desc, throw_flag);
return;
}
Expand All @@ -219,7 +222,8 @@ void Put(Handle<Error>& e, Handle<JSObject> O, Handle<String> P, Handle<JSValue>
if (!value.val()->IsUndefined()) {
Handle<PropertyDescriptor> desc = static_cast<Handle<PropertyDescriptor>>(value);
if (desc.val()->IsAccessorDescriptor()) {
log::PrintSource("Use parent prototype's setter");
if (log::Debugger::On())
log::PrintSource("Use parent prototype's setter");
Handle<JSValue> setter = desc.val()->Set();
assert(!setter.val()->IsUndefined());
Handle<JSObject> setter_obj = static_cast<Handle<JSObject>>(setter);
Expand Down Expand Up @@ -322,7 +326,8 @@ Handle<JSValue> DefaultValue(Handle<Error>& e, Handle<JSObject> O, std::u16strin
bool DefineOwnProperty(
Handle<Error>& e, Handle<JSObject> O, Handle<String> P, Handle<PropertyDescriptor> desc, bool throw_flag
) {
log::PrintSource("enter DefineOwnProperty " + P.ToString());
if (log::Debugger::On())
log::PrintSource("enter DefineOwnProperty " + P.ToString());
if (O.val()->IsArrayObject()) {
return DefineOwnProperty__Array(e, static_cast<Handle<ArrayObject>>(O), P, desc, throw_flag);
} else if (O.val()->IsArgumentsObject()) {
Expand Down Expand Up @@ -366,14 +371,17 @@ bool DefineOwnProperty__Base(
same = same && (desc.val()->Enumerable() == current_desc.val()->Enumerable());
if (same) return true; // 6
}
log::PrintSource("desc: " + desc.ToString() + ", current: " + current_desc.ToString());
if (log::Debugger::On())
log::PrintSource("desc: " + desc.ToString() + ", current: " + current_desc.ToString());
if (!current_desc.val()->Configurable()) { // 7
if (desc.val()->Configurable()) { // 7.a
log::PrintSource("DefineOwnProperty: " + P.ToString() + " not configurable, while new value configurable");
if (log::Debugger::On())
log::PrintSource("DefineOwnProperty: " + P.ToString() + " not configurable, while new value configurable");
goto reject;
}
if (desc.val()->HasEnumerable() && (desc.val()->Enumerable() != current_desc.val()->Enumerable())) { // 7.b
log::PrintSource("DefineOwnProperty: " + P.ToString() + " enumerable value differ");
if (log::Debugger::On())
log::PrintSource("DefineOwnProperty: " + P.ToString() + " enumerable value differ");
goto reject;
}
}
Expand Down Expand Up @@ -410,13 +418,15 @@ bool DefineOwnProperty__Base(
}
}
}
log::PrintSource("DefineOwnProperty: " + P.ToString() + " is set to " + desc.val()->Value().ToString());
if (log::Debugger::On())
log::PrintSource("DefineOwnProperty: " + P.ToString() + " is set to " + desc.val()->Value().ToString());
// 12.
current_desc.val()->Set(desc);
// 13.
return true;
reject:
log::PrintSource("DefineOwnProperty reject");
if (log::Debugger::On())
log::PrintSource("DefineOwnProperty reject");
if (throw_flag) {
e = Error::TypeError();
}
Expand Down Expand Up @@ -493,7 +503,8 @@ bool DefineOwnProperty__Array(
}
return DefineOwnProperty__Base(e, O, P, desc, throw_flag);
reject:
log::PrintSource("Array::DefineOwnProperty reject " + P.ToString() + " " + desc.ToString());
if (log::Debugger::On())
log::PrintSource("Array::DefineOwnProperty reject " + P.ToString() + " " + desc.ToString());
if (throw_flag) {
e = Error::TypeError();
}
Expand Down Expand Up @@ -591,7 +602,8 @@ void AddValueProperty(
Handle<JSObject> O, Handle<String> name, Handle<JSValue> value, bool writable,
bool enumerable, bool configurable
) {
log::PrintSource("AddValueProperty " + name.ToString() + " to " + value.ToString());
if (log::Debugger::On())
log::PrintSource("AddValueProperty " + name.ToString() + " to " + value.ToString());
Handle<PropertyDescriptor> desc = PropertyDescriptor::New();
desc.val()->SetDataDescriptor(value, writable, enumerable, configurable);
// This should just like named_properties_[name] = desc
Expand Down
6 changes: 4 additions & 2 deletions es/types/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class Reference : public JSValue {
};

Handle<JSValue> GetValue(Handle<Error>& e, Handle<JSValue> V) {
log::PrintSource("GetValue V:" + V.ToString());
if (log::Debugger::On())
log::PrintSource("GetValue V:" + V.ToString());
if (!V.val()->IsReference()) {
return V;
}
Expand Down Expand Up @@ -97,7 +98,8 @@ Handle<JSValue> GetValue(Handle<Error>& e, Handle<JSValue> V) {
}

void PutValue(Handle<Error>& e, Handle<JSValue> V, Handle<JSValue> W) {
log::PrintSource("PutValue V: " + V.ToString() + ", W: " + W.ToString());
if (log::Debugger::On())
log::PrintSource("PutValue V: " + V.ToString() + ", W: " + W.ToString());
if (!V.val()->IsReference()) {
e = Error::ReferenceError(u"put value to non-reference.");
return;
Expand Down
10 changes: 4 additions & 6 deletions es/utils/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ class Tracker {
};

void PrintSource(std::string comment, std::u16string str = u"", std::string postfix = "") {
if (Debugger::On()) {
std::cout << comment;
for (const auto& c: str)
std::cout << static_cast<char>(c);
std::cout << postfix << std::endl;
}
std::cout << comment;
for (const auto& c: str)
std::cout << static_cast<char>(c);
std::cout << postfix << std::endl;
}

std::string ToString(std::u16string str) {
Expand Down

0 comments on commit fb47829

Please sign in to comment.