stdlib Error

31 views
Skip to first unread message

wif...@gmail.com

unread,
Jan 20, 2015, 10:04:17 AM1/20/15
to rapyd...@googlegroups.com

I'm experimenting with RapydScript for possible use in a large project I'm working on. It looks really nice so far. Thanks for the work you put in to it.

I am having a problem trying to use stdlib.js. I am loading it along with the rest of my source in index.html instead of using import.

I am getting "Uncaught ReferenceError: _$rapyd$_extends is not defined" in both Chrome and FireFox (Ubuntu 14.04).

I copied stdlib.pyj in to my project directory and it gets compiled by make along with my other files. Everything works fine if I don't include stdlib.js.

Alexander Tsepkov

unread,
Jan 20, 2015, 10:23:50 AM1/20/15
to wif...@gmail.com, RapydScript
_$rapyd$_extends is part of baselib, so you don't need stdlib for that functionality. But if I understand correctly, you're seeing issues when you add stdlib via "<script src='stdlib.js'></script>" but not when using "import stdlib" in the code? It is odd that stdlib is affecting this at all, which compile flags do you use?

Alexander Tsepkov

unread,
Jan 20, 2015, 2:11:12 PM1/20/15
to Wiff, RapydScript
baselib gets generated automatically by the compiler unless you use -m/--omit-baselib option for your compilation, you do not need to import it. Are you sure you don't have that flag set? I actually don't recommend this option unless the developer understands how baselib works and has a good reason for omitting it.

baselib is a clever way for the compiler to only load the extra logic that you need, things that you would take for granted in Python and are too common for stdlib but are missing from JavaScript. This includes things like enumerate, 'for i in x', etc. _$rapyd$_extends, for example, is a background method for enforcing proper inheritance in classes. Whereas stdlib is more for appending pythonic methods to JavaScript objects (i.e. str.zfill(), dict.keys()). You almost always want the compiler to handle baselib additions for you, whereas you may wish to use something like underscore.js in place of stdlib.

The -m/--omit-baselib flag was added to exclude the automatic addition of this logic at the request of another developer who was compiling multiple RapydScript modules separately and didn't want the overhead of having the same declarations duplicated in all of these compilations. Instead he wanted to include baselib.js in the page, which all of the modules would reuse. This is currently the only use case I see for omitting baselib.

On Tue, Jan 20, 2015 at 12:41 PM, Wiff <wif...@gmail.com> wrote:

I found the problem. I also have to include "<script src='baselib.js'>...".

I am converting an existing project and at this point am compiling each file individually with make. None of them import baselib so it was never getting loaded.

But "import baselib" doesn't seem to work so I'm a little confused on how baselib gets included in a normal project. The baselib code is not added to any of my files.


On 01/20/2015 11:00 AM, Wiff wrote:

Sorry, I was not very clear.

I am including the file via "<script src='stdlib.js'>..."

I had not tried to use "import stdlib", but I just did and I still get the error.

I am using:

rapydscript $1 -p --comments all --screw-ie8 --output $2 

I tried it without screw-ie8 and there was no change.

I am not yet trying to use any functionality of stdlib. I gets the error when the file is read:


"Uncaught ReferenceError: _$rapyd$_extends is not defined"

function IndexError() {
    this.__init__.apply(this, arguments);
}
_$rapyd$_extends(IndexError, Error);
IndexError.prototype.__init__ = function __init__(message){
    var self = this;

Wiff

unread,
Jan 20, 2015, 2:52:28 PM1/20/15
to Alexander Tsepkov, RapydScript

I see what is going on now.

It's the "--comments all" that is doing it.

If I make a file like this:

#---------------------------
#Here are some comments
#---------------------------
a = "Test"
print(len(a))

And compile it without --comments all, it works properly.

If I use --comments all, I get only:

//---------------------------
//Here are some comments
//---------------------------
a = "Test";
_$rapyd$_print(len(a));

It doesn't include the definition of print and len or the "(function(){" wrapper.

If I use --comments all but there are no comments at the top, it works ok. It seems to only be an issue when the comments are at the top of the file.

It also doesn't seem to keep comments at the end of the file in any case.


>The -m/--omit-baselib flag was added to exclude the automatic addition of this logic at the request of
>another developer who was compiling multiple RapydScript modules separately and didn't want the
>overhead of having the same declarations duplicated in all of these compilations.

This is how I am using it also. It is a large project with dozens of files. Duplicating that code in each file would not be good.

Alexander Tsepkov

unread,
Jan 20, 2015, 4:43:46 PM1/20/15
to Wiff, RapydScript
I see, --comments option is something I haven't tested much, so it's possible I broke it somewhere along the way. In fact, I don't even document it, because I got it as a freebie from using UglifyJS as a base. Is there a reason why you need to include those files in HTML separately rather than importing them all through RapydScript itself, which would take care of dependencies and baselib for you?

There are a few disadvantages to compiling files separately:
- you'll need to manually include baselib unless you want it duplicated (compiling them together will cause no duplication)
- you prevent RapydScript from automatically detecting classes from other modules (which will require you to either use the "new" keyword or declare @external class in each module that uses it)
- if you want those modules to interact (in which case you should almost definitely compile them together), you'll need to use --bare compile option to prevent them from executing in a private scope (this option will leak all of your code into global scope)

Wiff

unread,
Jan 20, 2015, 5:00:39 PM1/20/15
to Alexander Tsepkov, RapydScript

On 01/20/2015 04:43 PM, Alexander Tsepkov wrote:
I see, --comments option is something I haven't tested much, so it's possible I broke it somewhere along the way. In fact, I don't even document it, because I got it as a freebie from using UglifyJS as a base. Is there a reason why you need to include those files in HTML separately rather than importing them all through RapydScript itself, which would take care of dependencies and baselib for you?
This is an existing project that uses another class system. For this project I'm using RapydScript to make it easier for me to type, read and maintain and I'm not using Python classes at all. The server is in Python and it's easier to mentally switch between them with this.

There are a few disadvantages to compiling files separately:
- you'll need to manually include baselib unless you want it duplicated (compiling them together will cause no duplication)
- you prevent RapydScript from automatically detecting classes from other modules (which will require you to either use the "new" keyword or declare @external class in each module that uses it)
- if you want those modules to interact (in which case you should almost definitely compile them together), you'll need to use --bare compile option to prevent them from executing in a private scope (this option will leak all of your code into global scope)
All of the files already have wrappers around them. I understand about "new" and don't mind having to use it.

My goal right now is to determine if it's worth the trouble for this project, since I'm not taking full advantage of the classes and features. Or, if it would be worth starting over with the Python class system.

I did come across one thing that is making translating the js files a real headache. I'll start a new topic for that.
Reply all
Reply to author
Forward
0 new messages