Path: blob/main/python/pylang/tools/embedded_compiler.js
1396 views
/* vim:fileencoding=utf-81*2* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>3*4* Distributed under terms of the BSD license5*/6"use strict"; /*jshint node:true */78var has_prop = Object.prototype.hasOwnProperty.call.bind(9Object.prototype.hasOwnProperty10);1112module.exports = function (compiler, baselib, runjs, name) {13var LINE_CONTINUATION_CHARS = ":\\";14runjs = runjs || eval;15runjs(print_ast(compiler.parse(""), true));16runjs('var __name__ = "' + (name || "__embedded__") + '";');1718function print_ast(19ast,20keep_baselib,21keep_docstrings,22private_scope,23write_name24) {25var output_options = {26omit_baselib: !keep_baselib,27write_name: !!write_name,28private_scope: !!private_scope,29beautify: true,30keep_docstrings: keep_docstrings,31};32if (keep_baselib) output_options.baselib_plain = baselib;33var output = new compiler.OutputStream(output_options);34ast.print(output);35return output.get();36}3738return {39toplevel: null,4041compile: (code, opts) => {42opts = opts || {};43var classes = this.toplevel ? this.toplevel.classes : undefined;44var scoped_flags = this.toplevel ? this.toplevel.scoped_flags : undefined;45this.toplevel = compiler.parse(code, {46filename: opts.filename || "<embedded>",47basedir: "__stdlib__",48classes: classes,49scoped_flags: scoped_flags,50discard_asserts: opts.discard_asserts,51});52var ans = print_ast(53this.toplevel,54opts.keep_baselib,55opts.keep_docstrings,56opts.private_scope,57opts.write_name58);59if (classes) {60var class_exports = {};61var self = this;62this.toplevel.exports.forEach(function (name) {63class_exports[name] = true;64});65Object.getOwnPropertyNames(classes).forEach(function (name) {66if (67!has_prop(class_exports, name) &&68!has_prop(self.toplevel.classes, name)69)70self.toplevel.classes[name] = classes[name];71});72}73scoped_flags = this.toplevel.scoped_flags;7475return ans;76},77};78};798081