@@ -480,38 +480,37 @@ Maybe<uv_file> CheckFile(const std::string& path,
480480 uv_fs_req_cleanup (&fs_req);
481481
482482 if (is_directory) {
483- uv_fs_close (nullptr , &fs_req, fd, nullptr );
483+ CHECK_EQ ( 0 , uv_fs_close (nullptr , &fs_req, fd, nullptr ) );
484484 uv_fs_req_cleanup (&fs_req);
485485 return Nothing<uv_file>();
486486 }
487487
488488 if (opt == CLOSE_AFTER_CHECK) {
489- uv_fs_close (nullptr , &fs_req, fd, nullptr );
489+ CHECK_EQ ( 0 , uv_fs_close (nullptr , &fs_req, fd, nullptr ) );
490490 uv_fs_req_cleanup (&fs_req);
491491 }
492492
493493 return Just (fd);
494494}
495495
496- PackageConfig emptyPackage = { false , false , " " };
497- std::unordered_map<std::string, PackageConfig> pjson_cache_;
496+ const Environment::PackageConfig& GetPackageConfig (Environment* env,
497+ const std::string path) {
498+ static const Environment::PackageConfig kEmptyPackage = { false , false , " " };
498499
499- PackageConfig& GetPackageConfig (Environment* env, const std::string path) {
500- auto existing = pjson_cache_.find (path);
501- if (existing != pjson_cache_.end ()) {
500+ auto existing = env->package_json_cache .find (path);
501+ if (existing != env->package_json_cache .end ())
502502 return existing->second ;
503- }
504503 Maybe<uv_file> check = CheckFile (path, LEAVE_OPEN_AFTER_CHECK);
505- if (check.IsNothing ()) {
506- return pjson_cache_[path] =
507- emptyPackage;
508- }
504+ if (check.IsNothing ())
505+ return kEmptyPackage ;
509506
510507 Isolate* isolate = env->isolate ();
511- Local<Context> context = isolate->GetCurrentContext ();
508+
509+ v8::HandleScope handle_scope (isolate);
510+
512511 std::string pkg_src = ReadFile (check.FromJust ());
513512 uv_fs_t fs_req;
514- uv_fs_close (nullptr , &fs_req, check.FromJust (), nullptr );
513+ CHECK_EQ ( 0 , uv_fs_close (nullptr , &fs_req, check.FromJust (), nullptr ) );
515514 uv_fs_req_cleanup (&fs_req);
516515
517516 // It's not okay for the called of this method to not be able to tell
@@ -523,27 +522,28 @@ PackageConfig& GetPackageConfig(Environment* env, const std::string path) {
523522 pkg_src.c_str (),
524523 v8::NewStringType::kNormal ,
525524 pkg_src.length ()).ToLocal (&src)) {
526- return pjson_cache_[path] =
527- emptyPackage;
525+ return env->package_json_cache [path] = kEmptyPackage ;
528526 }
529527
530- Local<Value> pkg_json;
531- if (!JSON::Parse (context, src).ToLocal (&pkg_json) || !pkg_json->IsObject ())
532- return pjson_cache_[path] =
533- emptyPackage;
528+ Local<Value> pkg_json_v;
529+ Local<Object> pkg_json;
530+
531+ if (!JSON::Parse (env->context (), src).ToLocal (&pkg_json_v) ||
532+ !pkg_json_v->ToObject (env->context ()).ToLocal (&pkg_json))
533+ return kEmptyPackage ; // Exception pending.
534+
534535 Local<Value> pkg_main;
535536 bool has_main = false ;
536537 std::string main_std;
537- if (pkg_json. As <Object>() ->Get (context, env->main_string ())
538+ if (pkg_json->Get (env-> context () , env->main_string ())
538539 .ToLocal (&pkg_main) && pkg_main->IsString ()) {
539540 has_main = true ;
540541 Utf8Value main_utf8 (isolate, pkg_main.As <String>());
541542 main_std = std::string (*main_utf8, main_utf8.length ());
542543 }
543544
544- PackageConfig pjson = { true , has_main, main_std };
545- return pjson_cache_[path] =
546- pjson;
545+ Environment::PackageConfig pjson = { true , has_main, main_std };
546+ return env->package_json_cache [path] = pjson;
547547}
548548
549549enum ResolveExtensionsOptions {
@@ -579,7 +579,8 @@ inline Maybe<URL> ResolveIndex(const URL& search) {
579579Maybe<URL> ResolveMain (Environment* env, const URL& search) {
580580 URL pkg (" package.json" , &search);
581581
582- PackageConfig pjson = GetPackageConfig (env, pkg.ToFilePath ());
582+ const Environment::PackageConfig& pjson =
583+ GetPackageConfig (env, pkg.ToFilePath ());
583584 if (!pjson.exists || !pjson.has_main ) {
584585 return Nothing<URL>();
585586 }
@@ -596,7 +597,8 @@ Maybe<URL> ResolveModule(Environment* env,
596597 URL dir (" " );
597598 do {
598599 dir = parent;
599- Maybe<URL> check = Resolve (env, " ./node_modules/" + specifier, dir, true );
600+ Maybe<URL> check =
601+ Resolve (env, " ./node_modules/" + specifier, dir, IgnoreMain);
600602 if (!check.IsNothing ()) {
601603 const size_t limit = specifier.find (' /' );
602604 const size_t spec_len =
@@ -618,7 +620,7 @@ Maybe<URL> ResolveModule(Environment* env,
618620
619621Maybe<URL> ResolveDirectory (Environment* env,
620622 const URL& search,
621- bool check_pjson_main) {
623+ package_main_check check_pjson_main) {
622624 if (check_pjson_main) {
623625 Maybe<URL> main = ResolveMain (env, search);
624626 if (!main.IsNothing ())
@@ -632,7 +634,7 @@ Maybe<URL> ResolveDirectory(Environment* env,
632634Maybe<URL> Resolve (Environment* env,
633635 const std::string& specifier,
634636 const URL& base,
635- bool check_pjson_main) {
637+ package_main_check check_pjson_main) {
636638 URL pure_url (specifier);
637639 if (!(pure_url.flags () & URL_FLAGS_FAILED)) {
638640 // just check existence, without altering
0 commit comments