@@ -111,6 +111,14 @@ api.process = async ({
111111 } else if ( protectedMode === 'warn' ) {
112112 // FIXME: remove logging and use a handler
113113 console . warn ( 'WARNING: invalid context nullification' ) ;
114+
115+ // get processed context from cache if available
116+ const processed = resolvedContext . getProcessed ( activeCtx ) ;
117+ if ( processed ) {
118+ rval = activeCtx = processed ;
119+ continue ;
120+ }
121+
114122 const oldActiveCtx = activeCtx ;
115123 // copy all protected term definitions to fresh initial context
116124 rval = activeCtx = api . getInitialContext ( options ) . clone ( ) ;
@@ -323,19 +331,33 @@ api.process = async ({
323331 'jsonld.SyntaxError' ,
324332 { code : 'invalid remote context' , context : localCtx } ) ;
325333 }
326- const importCtx = resolvedImport [ 0 ] . document ;
327- if ( '@import' in importCtx ) {
328- throw new JsonLdError (
329- 'Invalid JSON-LD syntax; imported context must not include @import.' ,
330- 'jsonld.SyntaxError' ,
331- { code : 'invalid context entry' , context : localCtx } ) ;
332- }
334+ const processedImport = resolvedImport [ 0 ] . getProcessed ( activeCtx ) ;
335+ if ( processedImport ) {
336+ // Note: if the same context were used in this active context
337+ // as a reference context, then processed_input might not
338+ // be a dict.
339+ ctx = processedImport ;
340+ } else {
341+ const importCtx = resolvedImport [ 0 ] . document ;
342+ if ( '@import' in importCtx ) {
343+ throw new JsonLdError (
344+ 'Invalid JSON-LD syntax; imported context must not include @import.' ,
345+ 'jsonld.SyntaxError' ,
346+ { code : 'invalid context entry' , context : localCtx } ) ;
347+ }
333348
334- // merge ctx into importCtx and replace rval with the result
335- for ( const key in importCtx ) {
336- if ( ! ctx . hasOwnProperty ( key ) ) {
337- ctx [ key ] = importCtx [ key ] ;
349+ // merge ctx into importCtx and replace rval with the result
350+ for ( const key in importCtx ) {
351+ if ( ! ctx . hasOwnProperty ( key ) ) {
352+ ctx [ key ] = importCtx [ key ] ;
353+ }
338354 }
355+
356+ // Note: this could potenially conflict if the import
357+ // were used in the same active context as a referenced
358+ // context and an import. In this case, we
359+ // could override the cached result, but seems unlikely.
360+ resolvedImport [ 0 ] . setProcessed ( activeCtx , ctx ) ;
339361 }
340362
341363 defined . set ( '@import' , true ) ;
0 commit comments