正在从Javascript调用Dart。。。是虫子吗?
在编译以下漂亮的代码时正在从Javascript调用Dart。。。是虫子吗?,dart,interop,Dart,Interop,在编译以下漂亮的代码时 @JS() library nodejs; import 'package:js/js.dart'; @JS() class Exports { external set functionName(Function function); } @JS() external Exports get exports; void _someDartFunction(dynamic s) { print('Hello from Dirt!'); } void ma
@JS()
library nodejs;
import 'package:js/js.dart';
@JS()
class Exports {
external set functionName(Function function);
}
@JS()
external Exports get exports;
void _someDartFunction(dynamic s) {
print('Hello from Dirt!');
}
void main(List<String> arguments) {
exports.functionName = allowInterop(_someDartFunction);
}
结节被称为“nodejs”(在这个上下文中名称不是问题!)。npm使用合适的package.json对模块进行初始化
然后我启动nodejs并需要模块
$ nodejs
> var n = require("nodejs")
ReferenceError: self is not defined
at Object.main (/home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:2544:28)
at /home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:3569:9
at init.currentScript (/home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:3549:7)
at dartProgram (/home/buergerling/Schreibtisch/dart/nodejs1/node_modules/njs/index.js:3564:5)
得到一个错误。可以通过从编译的index.js文件中取消self
来解决此问题
这里可以发现问题:
...
main: function($arguments) {
var t1 = type$.Function;
J.set$functionName$x(self.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
J.set$functionName$x(self.module.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
self.functionName = P.allowInterop(D.main___someDartFunction$closure(), t1);
P.print("Hello from Dirt!");
},
Exports: function Exports() {
}
...
将self.exports
更改为exports
可以解决问题,模块可以按预期加载和使用
有比手工编辑编译后的文件更优雅的解决方案吗?没有。不建议使用dart编写js库。
...
main: function($arguments) {
var t1 = type$.Function;
J.set$functionName$x(self.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
J.set$functionName$x(self.module.exports, P.allowInterop(D.main___someDartFunction$closure(), t1));
self.functionName = P.allowInterop(D.main___someDartFunction$closure(), t1);
P.print("Hello from Dirt!");
},
Exports: function Exports() {
}
...