1

我正在尝试使用 node.js v9.10.1 中的 ES 6 模块运行程序,使用该--experimental-modules选项。请注意,不使用 ES 6 模块的版本运行良好。

该模块是使用Transcrypt Python to JavaScript 编译器分支模块生成的。

它有以下代码:

// Transcrypt'ed from Python, 2018-03-31 10:50:58
var time = {};
import {__envir__, __nest__, __init__, __get__, __getcm__, __getsm__, py_metatype, object, __class__, __pragma__, __call__, __kwargtrans__, __globals__, __super__, property, __setProperty__, assert, __merge__, dir, setattr, getattr, hasattr, delattr, __in__, __specialattrib__, len, __i__, __k__, __t__, float, int, bool, py_typeof, issubclass, isinstance, callable, repr, chr, ord, max, min, round, __jsUsePyNext__, __pyUseJsNext__, py_iter, py_next, __PyIterator__, __JsIterator__, py_reversed, zip, range, any, all, sum, enumerate, copy, deepcopy, list, tuple, set, bytearray, bytes, str, dict, __jsmod__, __mod__, __pow__, __neg__, __matmul__, __mul__, __truediv__, __floordiv__, __add__, __sub__, __lshift__, __rshift__, __or__, __xor__, __and__, __eq__, __ne__, __lt__, __le__, __gt__, __ge__, __imatmul__, __ipow__, __ijsmod__, __imod__, __imul__, __idiv__, __iadd__, __isub__, __ilshift__, __irshift__, __ior__, __ixor__, __iand__, __getitem__, __setitem__, __getslice__, __setslice__, BaseException, Exception, IterableError, StopIteration, ValueError, KeyError, AssertionError, NotImplementedError, IndexError, AttributeError, py_TypeError, Warning, UserWarning, DeprecationWarning, RuntimeWarning, __sort__, sorted, map, filter, divmod, __Terminal__, __terminal__, print} from './org.transcrypt.__runtime__.js';
var __name__ = '__main__';
import * as __module_time__ from './time.js';
__nest__ (time, '', __module_time__);
export var http = require ('http');

export var Demo =  __class__ ('Demo', [object], {
    __module__: __name__,
    texts: tuple (['Welcome to the world of node.js', 'You can have your cake and eat it', "Use node's ecosystem while programming in Python", 'Using node.js from Transcrypt is easy', 'Take a Python ride into the node.js world']),
    get __init__ () {return __get__ (this, function (self, port) {
        print ('Demo server started on port', port);
        self.server = http.createServer (self.serve);
        self.server.listen (port);
        self.oldIndex = 0;
        self.newIndex = 0;
        self.count = 0;
    });},
    get serve () {return __get__ (this, function (self, request, response) {
        time.__adapt__ (request);
        response.writeHead (200);
        print ('Serving page', self.count);
        self.count++;
        while (self.newIndex == self.oldIndex) {
            self.newIndex = int (Math.random () * len (self.texts));
        }
        self.oldIndex = self.newIndex;
        response.end ('<h1>{}</h1><h1>{}</h1>'.format (self.texts [self.newIndex], time.localtime ()));
    });}
});
export var demo = Demo (8080);

错误报告说:

D:\activ_tosh\geatec\transcrypt\qquick\Transcrypt\transcrypt\demos\nodejs_demo\__target__\nodejs_demo.js:3
import {__envir__, __nest__, __init__, __get__, __getcm__, __getsm__, py_metatype, object, __class__, __pragma__, __call__, __kwargtrans__, __globals__, __super__, property, __setProperty__, assert, __merge__, dir, setattr, getattr, hasattr, delattr, __in__, __specialattrib__, len, __i__, __k__, __t__, float, int, bool, py_typeof, issubclass, isinstance, callable, repr, chr, ord, max, min, round, __jsUsePyNext__, __pyUseJsNext__, py_iter, py_next, __PyIterator__, __JsIterator__, py_reversed, zip, range, any, all, sum, enumerate, copy, deepcopy, list, tuple, set, bytearray, bytes, str, dict, __jsmod__, __mod__, __pow__, __neg__, __matmul__, __mul__, __truediv__, __floordiv__, __add__, __sub__, __lshift__, __rshift__, __or__, __xor__, __and__, __eq__, __ne__, __lt__, __le__, __gt__, __ge__, __imatmul__, __ipow__, __ijsmod__, __imod__, __imul__, __idiv__, __iadd__, __isub__, __ilshift__, __irshift__, __io

SyntaxError: Unexpected token {
    at new Script (vm.js:51:7)
    at createScript (vm.js:136:10)
    at Object.runInThisContext (vm.js:197:10)
    at Module._compile (module.js:613:28)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at createDynamicModule (internal/loader/Translators.js:50:15)
    at setExecutor (internal/loader/CreateDynamicModule.js:49:23)

不太相关,但为了完整起见,该模块是从以下位置转译的:

import time

http = require ('http')

class Demo:
    texts = (
        'Welcome to the world of node.js',
        'You can have your cake and eat it',
        'Use node\'s ecosystem while programming in Python',
        'Using node.js from Transcrypt is easy',
        'Take a Python ride into the node.js world'
    )

    def __init__ (self, port):
        print ('Demo server started on port', port)
        self.server = http.createServer (self.serve)
        self.server.listen (port)
        self.oldIndex = 0
        self.newIndex = 0
        self.count = 0

    def serve (self, request, response): 
        time.__adapt__ (request)

        response.writeHead (200)

        print ('Serving page', self.count)
        self.count += 1

        while self.newIndex == self.oldIndex:
            self.newIndex = int (Math.random () * len (self.texts))
        self.oldIndex = self.newIndex

        response.end ('<h1>{}</h1><h1>{}</h1>'.format (
            self.texts [self.newIndex], time.localtime ()
        ))

demo = Demo (8080)
4

0 回答 0