Skip to content

Commit

Permalink
Node版でrequireしたのオブジェクトにアクセスできるように #256
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Jan 15, 2018
1 parent c343aa7 commit 1f659f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/nako3.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class NakoCompiler {
this.__varslist = [{}, {}, {}] // このオブジェクトは変更しないこと (this.gen.__varslist と共有する)
this.__self = this
this.__vars = this.__varslist[2]
this.__module = {}
this.pluginfiles = {} // プラグインとして取り込んだファイルの一覧
// set this
lexer.compiler = this
Expand Down Expand Up @@ -137,6 +138,7 @@ class NakoCompiler {
let __varslist = this.__varslist
let __vars = this.__vars = this.__varslist[2] // eslint-disable-line
let __self = this.__self // eslint-disable-line
let __module = this.__module // eslint-disable-line
try {
__varslist[0].line = -1 // コンパイルエラーを調べるため
eval(js) // eslint-disable-line
Expand Down
6 changes: 4 additions & 2 deletions src/nako_gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class NakoGen {
return '' +
'var __varslist = this.__varslist = [{}, {}, {}];\n' +
'var __vars = this.__varslist[2];\n' +
'var __self = this;\n'
'var __self = this;\n' +
'var __module = {};\n'
}

static convLineno (node) {
Expand Down Expand Up @@ -903,8 +904,9 @@ class NakoGen {
}

convRequire (node) {
const moduleName = node.value
return NakoGen.convLineno(node.line) +
`require('${node.value}');\n`
`__module['${moduleName}'] = require('${moduleName}');\n`
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/plugin_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,17 @@ const PluginSystem = {
return a
}
},
'モジュール一覧取得': { // @取り込んだモジュール一覧を得る // @もじゅーるいちらんしゅとく
type: 'func',
josi: [],
fn: function (sys) {
const a = []
for (const f in sys.__module) {
a.push(f)
}
return a
}
},
'タイムゾーン設定': { // @タイムゾーンTをセットする(v1非互換)(タイムゾーンの一覧は https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List 参照) // @たいむぞーんせってい
type: 'func',
josi: [['で']],
Expand Down

0 comments on commit 1f659f4

Please # to comment.