GeezerVison is showing github.com — src: start annotating native code side effect · nodejs/node@51d613d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/cares_wrap.cc +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2148,8 +2148,8 @@ void Initialize(Local<Object> target, | 2148 | 2148 |
| 2149 | 2149 | env->SetMethod(target, "getaddrinfo", GetAddrInfo); | 2150 | 2150 | env->SetMethod(target, "getnameinfo", GetNameInfo); | 2151 | | - env->SetMethod(target, "isIPv6", IsIPv6); | 2152 | | - env->SetMethod(target, "canonicalizeIP", CanonicalizeIP); | | 2151 | + env->SetMethodNoSideEffect(target, "isIPv6", IsIPv6); | | 2152 | + env->SetMethodNoSideEffect(target, "canonicalizeIP", CanonicalizeIP); | 2153 | 2153 |
| 2154 | 2154 | env->SetMethod(target, "strerror", StrError); | 2155 | 2155 |
| @@ -2206,7 +2206,7 @@ void Initialize(Local<Object> target, | 2206 | 2206 | env->SetProtoMethod(channel_wrap, "querySoa", Query<QuerySoaWrap>); | 2207 | 2207 | env->SetProtoMethod(channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>); | 2208 | 2208 |
| 2209 | | - env->SetProtoMethod(channel_wrap, "getServers", GetServers); | | 2209 | + env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers); | 2210 | 2210 | env->SetProtoMethod(channel_wrap, "setServers", SetServers); | 2211 | 2211 | env->SetProtoMethod(channel_wrap, "cancel", Cancel); | 2212 | 2212 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +65-5Lines changed: 65 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -681,17 +681,41 @@ inline void Environment::ThrowUVException(int errorno, | 681 | 681 | inline v8::Local<v8::FunctionTemplate> | 682 | 682 | Environment::NewFunctionTemplate(v8::FunctionCallback callback, | 683 | 683 | v8::Local<v8::Signature> signature, | 684 | | - v8::ConstructorBehavior behavior) { | | 684 | + v8::ConstructorBehavior behavior, | | 685 | + v8::SideEffectType side_effect_type) { | 685 | 686 | v8::Local<v8::External> external = as_external(); | 686 | 687 | return v8::FunctionTemplate::New(isolate(), callback, external, | 687 | | - signature, 0, behavior); | | 688 | + signature, 0, behavior, side_effect_type); | 688 | 689 | } | 689 | 690 |
| 690 | 691 | inline void Environment::SetMethod(v8::Local<v8::Object> that, | 691 | 692 | const char* name, | 692 | 693 | v8::FunctionCallback callback) { | 693 | 694 | v8::Local<v8::Function> function = | 694 | | - NewFunctionTemplate(callback)->GetFunction(); | | 695 | + NewFunctionTemplate(callback, | | 696 | + v8::Local<v8::Signature>(), | | 697 | + // TODO(TimothyGu): Investigate if SetMethod is ever | | 698 | + // used for constructors. | | 699 | + v8::ConstructorBehavior::kAllow, | | 700 | + v8::SideEffectType::kHasSideEffect)->GetFunction(); | | 701 | + // kInternalized strings are created in the old space. | | 702 | + const v8::NewStringType type = v8::NewStringType::kInternalized; | | 703 | + v8::Local<v8::String> name_string = | | 704 | + v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked(); | | 705 | + that->Set(name_string, function); | | 706 | + function->SetName(name_string); // NODE_SET_METHOD() compatibility. | | 707 | +} | | 708 | + | | 709 | +inline void Environment::SetMethodNoSideEffect(v8::Local<v8::Object> that, | | 710 | + const char* name, | | 711 | + v8::FunctionCallback callback) { | | 712 | + v8::Local<v8::Function> function = | | 713 | + NewFunctionTemplate(callback, | | 714 | + v8::Local<v8::Signature>(), | | 715 | + // TODO(TimothyGu): Investigate if SetMethod is ever | | 716 | + // used for constructors. | | 717 | + v8::ConstructorBehavior::kAllow, | | 718 | + v8::SideEffectType::kHasNoSideEffect)->GetFunction(); | 695 | 719 | // kInternalized strings are created in the old space. | 696 | 720 | const v8::NewStringType type = v8::NewStringType::kInternalized; | 697 | 721 | v8::Local<v8::String> name_string = | @@ -705,7 +729,24 @@ inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that, | 705 | 729 | v8::FunctionCallback callback) { | 706 | 730 | v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that); | 707 | 731 | v8::Local<v8::FunctionTemplate> t = | 708 | | - NewFunctionTemplate(callback, signature, v8::ConstructorBehavior::kThrow); | | 732 | + NewFunctionTemplate(callback, signature, v8::ConstructorBehavior::kThrow, | | 733 | + v8::SideEffectType::kHasSideEffect); | | 734 | + // kInternalized strings are created in the old space. | | 735 | + const v8::NewStringType type = v8::NewStringType::kInternalized; | | 736 | + v8::Local<v8::String> name_string = | | 737 | + v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked(); | | 738 | + that->PrototypeTemplate()->Set(name_string, t); | | 739 | + t->SetClassName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility. | | 740 | +} | | 741 | + | | 742 | +inline void Environment::SetProtoMethodNoSideEffect( | | 743 | + v8::Local<v8::FunctionTemplate> that, | | 744 | + const char* name, | | 745 | + v8::FunctionCallback callback) { | | 746 | + v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that); | | 747 | + v8::Local<v8::FunctionTemplate> t = | | 748 | + NewFunctionTemplate(callback, signature, v8::ConstructorBehavior::kThrow, | | 749 | + v8::SideEffectType::kHasNoSideEffect); | 709 | 750 | // kInternalized strings are created in the old space. | 710 | 751 | const v8::NewStringType type = v8::NewStringType::kInternalized; | 711 | 752 | v8::Local<v8::String> name_string = | @@ -717,7 +758,26 @@ inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that, | 717 | 758 | inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that, | 718 | 759 | const char* name, | 719 | 760 | v8::FunctionCallback callback) { | 720 | | - v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback); | | 761 | + v8::Local<v8::FunctionTemplate> t = | | 762 | + NewFunctionTemplate(callback, v8::Local<v8::Signature>(), | | 763 | + v8::ConstructorBehavior::kAllow, | | 764 | + v8::SideEffectType::kHasSideEffect); | | 765 | + // kInternalized strings are created in the old space. | | 766 | + const v8::NewStringType type = v8::NewStringType::kInternalized; | | 767 | + v8::Local<v8::String> name_string = | | 768 | + v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked(); | | 769 | + that->Set(name_string, t); | | 770 | + t->SetClassName(name_string); // NODE_SET_METHOD() compatibility. | | 771 | +} | | 772 | + | | 773 | +inline void Environment::SetTemplateMethodNoSideEffect( | | 774 | + v8::Local<v8::FunctionTemplate> that, | | 775 | + const char* name, | | 776 | + v8::FunctionCallback callback) { | | 777 | + v8::Local<v8::FunctionTemplate> t = | | 778 | + NewFunctionTemplate(callback, v8::Local<v8::Signature>(), | | 779 | + v8::ConstructorBehavior::kAllow, | | 780 | + v8::SideEffectType::kHasNoSideEffect); | 721 | 781 | // kInternalized strings are created in the old space. | 722 | 782 | const v8::NewStringType type = v8::NewStringType::kInternalized; | 723 | 783 | v8::Local<v8::String> name_string = | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -751,19 +751,34 @@ class Environment { | 751 | 751 | v8::Local<v8::Signature> signature = | 752 | 752 | v8::Local<v8::Signature>(), | 753 | 753 | v8::ConstructorBehavior behavior = | 754 | | - v8::ConstructorBehavior::kAllow); | | 754 | + v8::ConstructorBehavior::kAllow, | | 755 | + v8::SideEffectType side_effect = | | 756 | + v8::SideEffectType::kHasSideEffect); | 755 | 757 |
| 756 | 758 | // Convenience methods for NewFunctionTemplate(). | 757 | 759 | inline void SetMethod(v8::Local<v8::Object> that, | 758 | 760 | const char* name, | 759 | 761 | v8::FunctionCallback callback); | | 762 | + | 760 | 763 | inline void SetProtoMethod(v8::Local<v8::FunctionTemplate> that, | 761 | 764 | const char* name, | 762 | 765 | v8::FunctionCallback callback); | 763 | 766 | inline void SetTemplateMethod(v8::Local<v8::FunctionTemplate> that, | 764 | 767 | const char* name, | 765 | 768 | v8::FunctionCallback callback); | 766 | 769 |
| | 770 | + // Safe variants denote the function has no side effects. | | 771 | + inline void SetMethodNoSideEffect(v8::Local<v8::Object> that, | | 772 | + const char* name, | | 773 | + v8::FunctionCallback callback); | | 774 | + inline void SetProtoMethodNoSideEffect(v8::Local<v8::FunctionTemplate> that, | | 775 | + const char* name, | | 776 | + v8::FunctionCallback callback); | | 777 | + inline void SetTemplateMethodNoSideEffect( | | 778 | + v8::Local<v8::FunctionTemplate> that, | | 779 | + const char* name, | | 780 | + v8::FunctionCallback callback); | | 781 | + | 767 | 782 | void BeforeExit(void (*cb)(void* arg), void* arg); | 768 | 783 | void RunBeforeExitCallbacks(); | 769 | 784 | void AtExit(void (*cb)(void* arg), void* arg); | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_js_api.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -287,7 +287,7 @@ void Initialize(Local<Object> target, Local<Value> unused, | 287 | 287 | if (agent->WillWaitForConnect()) | 288 | 288 | env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart); | 289 | 289 | env->SetMethod(target, "open", Open); | 290 | | - env->SetMethod(target, "url", Url); | | 290 | + env->SetMethodNoSideEffect(target, "url", Url); | 291 | 291 |
| 292 | 292 | env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper); | 293 | 293 | env->SetMethod(target, "asyncTaskCanceled", | @@ -298,7 +298,7 @@ void Initialize(Local<Object> target, Local<Value> unused, | 298 | 298 | InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>); | 299 | 299 |
| 300 | 300 | env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper); | 301 | | - env->SetMethod(target, "isEnabled", IsEnabled); | | 301 | + env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled); | 302 | 302 |
| 303 | 303 | auto conn_str = FIXED_ONE_BYTE_STRING(env->isolate(), "Connection"); | 304 | 304 | Local<FunctionTemplate> tmpl = | |
Collapse file Copy file name to clipboardExpand all lines: src/module_wrap.cc +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -789,11 +789,11 @@ void ModuleWrap::Initialize(Local<Object> target, | 789 | 789 | env->SetProtoMethod(tpl, "link", Link); | 790 | 790 | env->SetProtoMethod(tpl, "instantiate", Instantiate); | 791 | 791 | env->SetProtoMethod(tpl, "evaluate", Evaluate); | 792 | | - env->SetProtoMethod(tpl, "namespace", Namespace); | 793 | | - env->SetProtoMethod(tpl, "getStatus", GetStatus); | 794 | | - env->SetProtoMethod(tpl, "getError", GetError); | 795 | | - env->SetProtoMethod(tpl, "getStaticDependencySpecifiers", | 796 | | - GetStaticDependencySpecifiers); | | 792 | + env->SetProtoMethodNoSideEffect(tpl, "namespace", Namespace); | | 793 | + env->SetProtoMethodNoSideEffect(tpl, "getStatus", GetStatus); | | 794 | + env->SetProtoMethodNoSideEffect(tpl, "getError", GetError); | | 795 | + env->SetProtoMethodNoSideEffect(tpl, "getStaticDependencySpecifiers", | | 796 | + GetStaticDependencySpecifiers); | 797 | 797 |
| 798 | 798 | target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction()); | 799 | 799 | env->SetMethod(target, "resolve", Resolve); | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -160,6 +160,7 @@ using v8::Promise; | 160 | 160 | using v8::PropertyCallbackInfo; | 161 | 161 | using v8::ScriptOrigin; | 162 | 162 | using v8::SealHandleScope; | | 163 | +using v8::SideEffectType; | 163 | 164 | using v8::String; | 164 | 165 | using v8::TryCatch; | 165 | 166 | using v8::Undefined; | @@ -1969,7 +1970,10 @@ void SetupProcessObject(Environment* env, | 1969 | 1970 | title_string, | 1970 | 1971 | ProcessTitleGetter, | 1971 | 1972 | env->is_main_thread() ? ProcessTitleSetter : nullptr, | 1972 | | - env->as_external()).FromJust()); | | 1973 | + env->as_external(), | | 1974 | + v8::DEFAULT, | | 1975 | + v8::None, | | 1976 | + SideEffectType::kHasNoSideEffect).FromJust()); | 1973 | 1977 |
| 1974 | 1978 | // process.version | 1975 | 1979 | READONLY_PROPERTY(process, | @@ -2279,17 +2283,17 @@ void SetupProcessObject(Environment* env, | 2279 | 2283 | env->SetMethod(process, "_getActiveHandles", GetActiveHandles); | 2280 | 2284 | env->SetMethod(process, "_kill", Kill); | 2281 | 2285 |
| 2282 | | - env->SetMethod(process, "cwd", Cwd); | | 2286 | + env->SetMethodNoSideEffect(process, "cwd", Cwd); | 2283 | 2287 | env->SetMethod(process, "dlopen", DLOpen); | 2284 | 2288 | env->SetMethod(process, "reallyExit", Exit); | 2285 | | - env->SetMethod(process, "uptime", Uptime); | | 2289 | + env->SetMethodNoSideEffect(process, "uptime", Uptime); | 2286 | 2290 |
| 2287 | 2291 | #if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__) | 2288 | | - env->SetMethod(process, "getuid", GetUid); | 2289 | | - env->SetMethod(process, "geteuid", GetEUid); | 2290 | | - env->SetMethod(process, "getgid", GetGid); | 2291 | | - env->SetMethod(process, "getegid", GetEGid); | 2292 | | - env->SetMethod(process, "getgroups", GetGroups); | | 2292 | + env->SetMethodNoSideEffect(process, "getuid", GetUid); | | 2293 | + env->SetMethodNoSideEffect(process, "geteuid", GetEUid); | | 2294 | + env->SetMethodNoSideEffect(process, "getgid", GetGid); | | 2295 | + env->SetMethodNoSideEffect(process, "getegid", GetEGid); | | 2296 | + env->SetMethodNoSideEffect(process, "getgroups", GetGroups); | 2293 | 2297 | #endif // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__) | 2294 | 2298 | } | 2295 | 2299 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_buffer.cc +14-14Lines changed: 14 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1083,12 +1083,12 @@ void SetupBufferJS(const FunctionCallbackInfo<Value>& args) { | 1083 | 1083 | Local<Object> proto = args[0].As<Object>(); | 1084 | 1084 | env->set_buffer_prototype_object(proto); | 1085 | 1085 |
| 1086 | | - env->SetMethod(proto, "asciiSlice", StringSlice<ASCII>); | 1087 | | - env->SetMethod(proto, "base64Slice", StringSlice<BASE64>); | 1088 | | - env->SetMethod(proto, "latin1Slice", StringSlice<LATIN1>); | 1089 | | - env->SetMethod(proto, "hexSlice", StringSlice<HEX>); | 1090 | | - env->SetMethod(proto, "ucs2Slice", StringSlice<UCS2>); | 1091 | | - env->SetMethod(proto, "utf8Slice", StringSlice<UTF8>); | | 1086 | + env->SetMethodNoSideEffect(proto, "asciiSlice", StringSlice<ASCII>); | | 1087 | + env->SetMethodNoSideEffect(proto, "base64Slice", StringSlice<BASE64>); | | 1088 | + env->SetMethodNoSideEffect(proto, "latin1Slice", StringSlice<LATIN1>); | | 1089 | + env->SetMethodNoSideEffect(proto, "hexSlice", StringSlice<HEX>); | | 1090 | + env->SetMethodNoSideEffect(proto, "ucs2Slice", StringSlice<UCS2>); | | 1091 | + env->SetMethodNoSideEffect(proto, "utf8Slice", StringSlice<UTF8>); | 1092 | 1092 |
| 1093 | 1093 | env->SetMethod(proto, "asciiWrite", StringWrite<ASCII>); | 1094 | 1094 | env->SetMethod(proto, "base64Write", StringWrite<BASE64>); | @@ -1116,22 +1116,22 @@ void Initialize(Local<Object> target, | 1116 | 1116 | Environment* env = Environment::GetCurrent(context); | 1117 | 1117 |
| 1118 | 1118 | env->SetMethod(target, "setupBufferJS", SetupBufferJS); | 1119 | | - env->SetMethod(target, "createFromString", CreateFromString); | | 1119 | + env->SetMethodNoSideEffect(target, "createFromString", CreateFromString); | 1120 | 1120 |
| 1121 | | - env->SetMethod(target, "byteLengthUtf8", ByteLengthUtf8); | | 1121 | + env->SetMethodNoSideEffect(target, "byteLengthUtf8", ByteLengthUtf8); | 1122 | 1122 | env->SetMethod(target, "copy", Copy); | 1123 | | - env->SetMethod(target, "compare", Compare); | 1124 | | - env->SetMethod(target, "compareOffset", CompareOffset); | | 1123 | + env->SetMethodNoSideEffect(target, "compare", Compare); | | 1124 | + env->SetMethodNoSideEffect(target, "compareOffset", CompareOffset); | 1125 | 1125 | env->SetMethod(target, "fill", Fill); | 1126 | | - env->SetMethod(target, "indexOfBuffer", IndexOfBuffer); | 1127 | | - env->SetMethod(target, "indexOfNumber", IndexOfNumber); | 1128 | | - env->SetMethod(target, "indexOfString", IndexOfString); | | 1126 | + env->SetMethodNoSideEffect(target, "indexOfBuffer", IndexOfBuffer); | | 1127 | + env->SetMethodNoSideEffect(target, "indexOfNumber", IndexOfNumber); | | 1128 | + env->SetMethodNoSideEffect(target, "indexOfString", IndexOfString); | 1129 | 1129 |
| 1130 | 1130 | env->SetMethod(target, "swap16", Swap16); | 1131 | 1131 | env->SetMethod(target, "swap32", Swap32); | 1132 | 1132 | env->SetMethod(target, "swap64", Swap64); | 1133 | 1133 |
| 1134 | | - env->SetMethod(target, "encodeUtf8String", EncodeUtf8String); | | 1134 | + env->SetMethodNoSideEffect(target, "encodeUtf8String", EncodeUtf8String); | 1135 | 1135 |
| 1136 | 1136 | target->Set(env->context(), | 1137 | 1137 | FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"), | |
|