@@ -6,6 +6,11 @@ var version = require("./version");
6
6
var compilers = require ( "./compiler/compilers" ) ;
7
7
var { typecheck, printSignature } = require ( "./typecheck" ) ;
8
8
var { expandMacros, extractMacros } = require ( "./macro.js" ) ;
9
+ var { defaultImportReader } = require ( "./reader" ) ;
10
+
11
+ const defaultTrustedHosts = [
12
+ "https://raw.githubusercontent.com/LingDong-/wenyan-lang/master"
13
+ ] ;
9
14
10
15
function wy2tokens (
11
16
txt ,
@@ -213,6 +218,12 @@ function tokenRomanize(tokens, method) {
213
218
}
214
219
}
215
220
221
+ function defaultLogCallback ( x ) {
222
+ return typeof x == "string"
223
+ ? console . log ( x )
224
+ : console . dir ( x , { depth : null , maxArrayLength : null } ) ;
225
+ }
226
+
216
227
function tokens2asc (
217
228
tokens ,
218
229
assert = ( msg , pos , b ) => {
@@ -624,29 +635,6 @@ function pyWrapModule(name, src) {
624
635
return `#/*___wenyan_module_${ name } _start___*/\n${ src } \n#/*___wenyan_module_${ name } _end___*/\n` ;
625
636
}
626
637
627
- function defaultReader ( x ) {
628
- try {
629
- const fs = eval ( "require" ) ( "fs" ) ;
630
- try {
631
- return fs . readFileSync ( x + ".wy" ) . toString ( ) ;
632
- } catch ( e ) {
633
- var files = fs . readdirSync ( "./" ) ;
634
- for ( var i = 0 ; i < files . length ; i ++ ) {
635
- if ( fs . lstatSync ( files [ i ] ) . isDirectory ( ) ) {
636
- try {
637
- return fs . readFileSync ( files [ i ] + "/" + x + ".wy" ) . toString ( ) ;
638
- } catch ( e ) { }
639
- }
640
- }
641
- }
642
- console . log ( "Cannot import " , x ) ;
643
- } catch ( e ) {
644
- console . error (
645
- `Cannot import ${ x } , please specify the "reader" option in compile.`
646
- ) ;
647
- }
648
- }
649
-
650
638
function compile ( arg1 , arg2 , arg3 ) {
651
639
let options = { } ;
652
640
let txt = "" ;
@@ -663,17 +651,26 @@ function compile(arg1, arg2, arg3) {
663
651
const {
664
652
lang = "js" ,
665
653
romanizeIdentifiers = "none" ,
666
- resetVarCnt,
667
- logCallback = x =>
668
- typeof x == "string"
669
- ? console . log ( x )
670
- : console . dir ( x , { depth : null , maxArrayLength : null } ) ,
654
+ resetVarCnt = true ,
655
+ logCallback = defaultLogCallback ,
671
656
errorCallback = process . exit ,
672
657
lib = typeof STDLIB == "undefined" ? { } : STDLIB ,
673
- reader = defaultReader ,
674
- strict = false
658
+ reader = defaultImportReader ,
659
+ importPaths = [ ] ,
660
+ strict = false ,
661
+ allowHttp = false ,
662
+ trustedHosts = [ ] ,
663
+ requestTimeout = 2000
675
664
} = options ;
676
665
666
+ trustedHosts . push ( ...defaultTrustedHosts ) ;
667
+
668
+ const requestOptions = {
669
+ allowHttp,
670
+ trustedHosts,
671
+ requestTimeout
672
+ } ;
673
+
677
674
if ( resetVarCnt ) idenMap = { } ;
678
675
txt = ( txt || "" ) . replace ( / \r \n / g, "\n" ) ;
679
676
@@ -703,7 +700,13 @@ function compile(arg1, arg2, arg3) {
703
700
return 0 ;
704
701
}
705
702
706
- var macros = extractMacros ( txt , { lib, reader, lang } ) ;
703
+ var macros = extractMacros ( txt , {
704
+ lib,
705
+ reader,
706
+ lang,
707
+ importPaths,
708
+ requestOptions
709
+ } ) ;
707
710
txt = expandMacros ( txt , macros ) ;
708
711
709
712
logCallback ( "\n\n=== [PASS 0] EXPAND-MACROS ===" ) ;
@@ -751,19 +754,15 @@ function compile(arg1, arg2, arg3) {
751
754
} else if ( imports [ i ] in lib ) {
752
755
isrc = lib [ imports [ i ] ] ;
753
756
} else {
754
- isrc = reader ( imports [ i ] ) ;
757
+ isrc = reader ( imports [ i ] , importPaths , requestOptions ) ;
755
758
}
756
759
targ =
757
760
mwrapper (
758
761
imports [ i ] ,
759
762
compile ( isrc , {
760
- lang,
761
- romanizeIdentifiers,
763
+ ...options ,
762
764
resetVarCnt : false ,
763
- strict : false ,
764
- logCallback,
765
- errorCallback,
766
- lib
765
+ strict : false
767
766
} )
768
767
) + targ ;
769
768
}
0 commit comments