@@ -96,88 +96,83 @@ private static JSServiceArgs args(Path pluginPath, Optional<MongoClient> mclient
9696 LOGGER .trace ("Enabling require for service {} with require-cwd {} " , pluginPath , requireCwdPath );
9797 }
9898
99- // Context creation, eval and all Value member access must happen on the very
100- // same platform thread: a Value returned by eval() is only safely readable on
101- // the thread the context is entered on (see PolyglotThreadUtils), so the whole
102- // block below -- not just the eval() calls -- is dispatched as a single task.
99+ // During startup the calling thread is the JVM main thread (a genuine
100+ // platform thread), so no executor dispatch is needed.
103101 try {
104- return PolyglotThreadUtils .onPlatformThread (() -> {
105- var ctx = ContextQueue .newContext (engine (), "foo" , config , LOGGER , mclient , "" , contextOptions );
106- ctx .enter ();
107- try {
108- // check that the plugin script is js (use PluginsClassloader so js-language is visible)
109- final var language = PolyglotClassloaderHelper .withPluginsClassloaderResult (
110- () -> Source .findLanguage (pluginPath .toFile ()));
111-
112- if (!"js" .equals (language )) {
113- throw new IllegalArgumentException ("wrong js plugin, not javascript" );
114- }
115-
116- var sindexPath = pluginPath .toUri ().toString ();
117- LOGGER .debug ("Resolved plugin path for import: {}" , sindexPath );
118- var optionsScript = "import { options } from '" + sindexPath + "'; options;" ;
119- var optionsSource = Source .newBuilder (language , optionsScript , "optionsScript" ).mimeType ("application/javascript+module" ).build ();
120-
121- Value options ;
122-
123- try {
124- options = ctx .eval (optionsSource );
125- } catch (Throwable t ) {
126- if (t .getMessage () != null && t .getMessage ().contains ("Cannot load CommonJS module" )) {
127- throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ": " + t .getMessage ());
128- } else if (t .getMessage () != null && t .getMessage ().contains ("Access to host class" )) {
129- throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ": " + t .getMessage ());
130- } else {
131- throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ": " + t .getMessage () + ", " + PACKAGE_HINT );
132- }
133- }
134-
135- checkOptions (options , pluginPath );
136-
137- var name = options .getMember ("name" ).asString ();
138- var description = options .getMember ("description" ).asString ();
139- var uri = options .getMember ("uri" ).asString ();
140- var secured = !options .getMemberKeys ().contains ("secured" ) ? false : options .getMember ("secured" ).asBoolean ();
141- var matchPolicy = !options .getMemberKeys ().contains ("matchPolicy" ) ? MATCH_POLICY .PREFIX : MATCH_POLICY .valueOf (options .getMember ("matchPolicy" ).asString ());
142- String modulesReplacements = null ;
143-
144- if (options .getMemberKeys ().contains ("modulesReplacements" )) {
145- var sb = new StringBuilder ();
146-
147- options .getMember ("modulesReplacements" ).getMemberKeys ().stream ()
148- .forEach (k -> sb .append (k ).append (":" )
149- .append (options .getMember ("modulesReplacements" ).getMember (k ))
150- .append ("," ));
151-
152- modulesReplacements = sb .toString ();
153- }
154-
155- // ******** evaluate and check handle
156- var _handleScript = "import { handle } from '" + sindexPath + "'; handle;" ;
157- var handleSource = Source .newBuilder (language , _handleScript , "handleScript" ).mimeType ("application/javascript+module" ).build ();
158-
159- Value handle ;
160-
161- try {
162- handle = ctx .eval (handleSource );
163- } catch (Throwable t ) {
164- throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ", " + t .getMessage ());
165- }
166-
167- checkHandle (handle , pluginPath );
168-
169- return new JSServiceArgs (name , description , uri , secured , modulesReplacements , matchPolicy , handleSource , config , mclient , contextOptions );
170- } finally {
171- ctx .leave ();
172- ctx .close ();
102+ var ctx = ContextQueue .newContext (engine (), "foo" , config , LOGGER , mclient , "" , contextOptions );
103+ ctx .enter ();
104+ try {
105+ // check that the plugin script is js (use PluginsClassloader so js-language is visible)
106+ final var language = PolyglotClassloaderHelper .withPluginsClassloaderResult (
107+ () -> Source .findLanguage (pluginPath .toFile ()));
108+
109+ if (!"js" .equals (language )) {
110+ throw new IllegalArgumentException ("wrong js plugin, not javascript" );
111+ }
112+
113+ var sindexPath = pluginPath .toUri ().toString ();
114+ LOGGER .debug ("Resolved plugin path for import: {}" , sindexPath );
115+ var optionsScript = "import { options } from '" + sindexPath + "'; options;" ;
116+ var optionsSource = Source .newBuilder (language , optionsScript , "optionsScript" ).mimeType ("application/javascript+module" ).build ();
117+
118+ Value options ;
119+
120+ try {
121+ options = ctx .eval (optionsSource );
122+ } catch (Throwable t ) {
123+ if (t .getMessage () != null && t .getMessage ().contains ("Cannot load CommonJS module" )) {
124+ throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ": " + t .getMessage ());
125+ } else if (t .getMessage () != null && t .getMessage ().contains ("Access to host class" )) {
126+ throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ": " + t .getMessage ());
127+ } else {
128+ throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ": " + t .getMessage () + ", " + PACKAGE_HINT );
173129 }
174- });
175- } catch (RuntimeException re ) {
176- throw re ;
177- } catch (IOException ioe ) {
178- throw ioe ;
179- } catch (Exception e ) {
180- throw new IllegalStateException ("Error evaluating js service " + pluginPath .toAbsolutePath (), e );
130+ }
131+
132+ checkOptions (options , pluginPath );
133+
134+ var name = options .getMember ("name" ).asString ();
135+ var description = options .getMember ("description" ).asString ();
136+ var uri = options .getMember ("uri" ).asString ();
137+ var secured = !options .getMemberKeys ().contains ("secured" ) ? false : options .getMember ("secured" ).asBoolean ();
138+ var matchPolicy = !options .getMemberKeys ().contains ("matchPolicy" ) ? MATCH_POLICY .PREFIX : MATCH_POLICY .valueOf (options .getMember ("matchPolicy" ).asString ());
139+ String modulesReplacements = null ;
140+
141+ if (options .getMemberKeys ().contains ("modulesReplacements" )) {
142+ var sb = new StringBuilder ();
143+
144+ options .getMember ("modulesReplacements" ).getMemberKeys ().stream ()
145+ .forEach (k -> sb .append (k ).append (":" )
146+ .append (options .getMember ("modulesReplacements" ).getMember (k ))
147+ .append ("," ));
148+
149+ modulesReplacements = sb .toString ();
150+ }
151+
152+ // ******** evaluate and check handle
153+ var _handleScript = "import { handle } from '" + sindexPath + "'; handle;" ;
154+ var handleSource = Source .newBuilder (language , _handleScript , "handleScript" ).mimeType ("application/javascript+module" ).build ();
155+
156+ Value handle ;
157+
158+ try {
159+ handle = ctx .eval (handleSource );
160+ } catch (Throwable t ) {
161+ throw new IllegalArgumentException ("wrong js service " + pluginPath .toAbsolutePath () + ", " + t .getMessage ());
162+ }
163+
164+ checkHandle (handle , pluginPath );
165+
166+ return new JSServiceArgs (name , description , uri , secured , modulesReplacements , matchPolicy , handleSource , config , mclient , contextOptions );
167+ } finally {
168+ ctx .leave ();
169+ ctx .close ();
170+ }
171+ } catch (Throwable t ) {
172+ // DIAGNOSTIC: log full stack trace including PolyglotException chain
173+ LOGGER .error ("DIAGNOSTIC: full exception chain for {} [thread={}, class={}]:" ,
174+ pluginPath , Thread .currentThread ().getName (), t .getClass ().getName (), t );
175+ throw t ;
181176 }
182177 }
183178
0 commit comments