Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Can QScriptEngine read a tree of .js files at runtime?



  • I have several .js files that I would like a QScriptEngine to read together. All I can find in the documentation is examples of initializing QScriptEngine by evaluating stings containing JavaScript code. How would JavaScript's import statement work in that case? How would it know where to find the referenced by the import statement?

    For example, how would I go about reading in the following JavaScript files? Could I do it just by specifying I wanted 'birds.js' and 'cats.js' read in and let QScriptEngine find the other files on its own?

    //core.js
    
    function Animal(name) {
        this.name = name;
        this.toString = function() { this.name; }
    }
    
    
    
    //details/animals.js
    import * as Core from '/core.js';
    
    function Bird(name, birdseed) {
        Core.Animal.call(this, name);
        this.birdseed = birdseed;
    }
    Bird.prototype = new Core.Animal();
    
    
    function Cat(name, numLives) {
        Core.Animal.call(this, name);
        this.numLives = numLives;
    }
    Cat.prototype = new Core.Animal();
    
    
    
    //gallery/birds.js
    import {Bird} from '/details/animals.js';
    
    function BigBird(name, birdseed, height) {
        Bird.call(this, name, birdseed);
        this.height = height;
        this.toString = function() { this.name + " " + this.birdseed + " " + this.height; }
    }
    BigBird.prototype = new Bird();
    
    var marthaBird = new BigBird("Martha", 4);
    var fredBird = new BigBird("Fred", 5);
    
    
    
    //gallery/cats.js
    import {Cat} from '/details/animals.js';
    
    function Sabertooth(name, numLives, toothpaste) {
        Cat.call(this, name, numLives);
        this.toothpaste = toothpaste;
    }
    Sabertooth.prototype = new Cat();
    

Log in to reply