@@ -83,42 +83,56 @@ function handle_last_statement(
8383 code_user ,
8484 ast
8585) {
86- // is the very last character a semicolon?
87- const last_char_is_semicolon = code_user [ code_user . length - 1 ] == ";" ;
8886
8987 // get the last node
9088 const last_node = ast . body [ ast . body . length - 1 ] ;
9189
90+
9291 // if the last node is an expression statement
9392 // then we need to add a return statement
9493 // so that the expression gets returned
95- if ( ! last_char_is_semicolon && last_node . type == "ExpressionStatement" && last_node . expression . type != "AssignmentExpression" ) {
94+ if ( last_node . type == "ExpressionStatement" && last_node . expression . type != "AssignmentExpression" ) {
9695
97- const last_node_start = last_node . start ;
98- const last_node_end = last_node . end ;
96+ const last_node_expr_start = last_node . expression . start ;
97+ const last_node_expr_end = last_node . expression . end ;
9998
100- // remove the last node from the code
101- const modified_user_code = code_user . substring ( 0 , last_node_start ) + code_user . substring ( last_node_end ) ;
102- const code_of_last_node = code_user . substring ( last_node_start , last_node_end ) ;
99+ // "rest"
100+ const last_node_rest_end = last_node . end
101+ // search between last_node_expr_end and last_node_rest_end for a semicolon
102+ // if there is a semicolon then we dont need to add a return
103103
104- const extra_return_code = `return [${ code_of_last_node } ];` ;
104+ let semicolon_found = false ;
105+ for ( let i = last_node_expr_end ; i < last_node_rest_end ; i ++ ) {
106+ if ( code_user [ i ] == ";" ) {
107+ semicolon_found = true ;
108+ break ;
109+ }
110+ }
105111
112+ if ( ! semicolon_found ) {
113+ // remove the last node from the code
114+ const modified_user_code = code_user . substring ( 0 , last_node_expr_start ) + code_user . substring ( last_node_expr_end ) ;
115+ const code_of_last_node = code_user . substring ( last_node_expr_start , last_node_expr_end ) ;
106116
107- return {
108- with_return : true ,
109- modified_user_code : modified_user_code ,
110- extra_return_code : extra_return_code
117+
118+ const extra_return_code = `return [${ code_of_last_node } ];` ;
119+
120+
121+ return {
122+ with_return : true ,
123+ modified_user_code : modified_user_code ,
124+ extra_return_code : extra_return_code
125+ }
111126 }
112127 }
113- else
114- {
115- return {
116- with_return : false ,
117- modified_user_code : code_user ,
118- extra_return_code : ""
119- }
128+
129+ return {
130+ with_return : false ,
131+ modified_user_code : code_user ,
132+ extra_return_code : ""
120133 }
121134
135+
122136}
123137
124138function transform_import_source ( source ) {
@@ -317,7 +331,7 @@ function _configure() {
317331 msg += " " ;
318332 }
319333 }
320- Module . interpreter . publish_stream ( "stdout" , `${ msg } \n` ) ;
334+ Module . interpreter . publish_stream ( Module . get_request_context ( ) , "stdout" , `${ msg } \n` ) ;
321335 }
322336 // alias
323337 globalThis . pp = globalThis . pprint ;
@@ -330,7 +344,7 @@ function _configure() {
330344 msg += " " ;
331345 }
332346 }
333- Module . interpreter . publish_stream ( "stdout" , `${ msg } \n` ) ;
347+ Module . interpreter . publish_stream ( Module . get_request_context ( ) , "stdout" , `${ msg } \n` ) ;
334348 }
335349 console . error = function ( ... args ) {
336350 let msg = ""
@@ -340,29 +354,42 @@ function _configure() {
340354 msg += " " ;
341355 }
342356 }
343- Module . interpreter . publish_stream ( "stderr" , `${ msg } \n` ) ;
357+ Module . interpreter . publish_stream ( Module . get_request_context ( ) , "stderr" , `${ msg } \n` ) ;
344358 }
345359
346360 // add ijs to global scope
347361 globalThis [ "ijs" ] = Module [ "ijs" ] ;
348362
349363 Module . interpreter = Module . _get_interpreter ( ) ;
350364
351- Module . interpreter . publish_stream ( "stdout" , "Configured\n" ) ;
365+
366+ // the execute request context
367+ Module . _xrequest_context = null ;
352368}
353369
354370
371+ Module . get_request_context = function ( ) {
372+ return Module . _xrequest_context ;
373+ }
374+
355375Module . get_interpreter = function ( ) {
356376 return Module . interpreter ;
357377}
358378
359379
360- async function _call_user_code ( code ) {
380+ async function _call_user_code ( context , code ) {
361381
382+ // call _xrequest_context.delete() if it exists
383+ if ( Module . _xrequest_context && Module . _xrequest_context . delete ) {
384+ Module . _xrequest_context . delete ( ) ;
385+ }
386+ Module . _xrequest_context = context ;
362387
363388 try {
389+
364390 const ret = make_async_from_code ( code ) ;
365391 const async_function = ret . async_function ;
392+
366393 let result_promise = async_function ( ) ;
367394
368395 let data = { } ;
@@ -441,8 +468,6 @@ function complete_line(code_line){
441468 }
442469 }
443470 let pseudo_expression = code_line . substring ( code_begin ) ;
444-
445-
446471 // pseudo_expression is "fubar.b" "fubar", "fubar." or "fubar['aaa']"
447472
448473 // find part right of dot / bracket
@@ -469,16 +494,13 @@ function complete_line(code_line){
469494 curser_start += split_pos + 1 ;
470495 }
471496
472-
473-
474497 // find root object
475498 let root_object = globalThis ;
476499 if ( root_object_str != "" ) {
477500 try {
478501 root_object = eval ( root_object_str ) ;
479502 }
480503 catch ( err ) {
481- Module [ "_publish_stderr_stream" ] ( `${ err } \n` ) ;
482504 return {
483505 matches : [ ] ,
484506 cursor_start : curser_start ,
@@ -551,10 +573,10 @@ let ijs = {
551573 magic_imports : magic_imports ,
552574 display : {
553575 display : function ( data , metadata = { } , transient = { } ) {
554- Module . get_interpreter ( ) . display_data ( data , metadata , transient ) ;
576+ Module . get_interpreter ( ) . display_data ( Module . get_request_context ( ) , data , metadata , transient ) ;
555577 } ,
556578 update_display_data : function ( data , metadata = { } , transient = { } ) {
557- Module . get_interpreter ( ) . update_display_data ( data , metadata , transient ) ;
579+ Module . get_interpreter ( ) . update_display_data ( Module . get_request_context ( ) , data , metadata , transient ) ;
558580 } ,
559581 mime_type : function ( mime_type , data ) {
560582 this . display ( { mime_type : data } ) ;
@@ -636,7 +658,7 @@ let ijs = {
636658 }
637659 }
638660 catch ( err ) {
639- Module . interpreter . publish_stream ( "stderr" , `display error: ${ err } \n` ) ;
661+ Module . interpreter . publish_stream ( Module . get_request_context ( ) , "stderr" , `display error: ${ err } \n` ) ;
640662 }
641663 }
642664
0 commit comments