gfwilliams / tiny-js

Automatically exported from code.google.com/p/tiny-js
MIT License
532 stars 88 forks source link

Array.push - not defined? #16

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Array push function seems undefined.
A simple script like this :

out = new Array();
out.push();

gives the following error:

ReferenceError: push is undefined at Line:2 Column:9

the c++ code I use for this is the following(from the samples):
void test()
{
    CTinyJS *js = new CTinyJS();

    try {
        js->execute(GetCode());
    } catch (CScriptException *e) {
        printf("%s\n", e->toString().c_str());
    }
}

I am using 42tiny-js.
compiler : Visual Studio 2012
OS: Windows 7 (64 bit)

I am trying this library for the first time. Am I  missing something simple.

Regards

Original issue reported on code.google.com by mertbuyu...@gmail.com on 11 Apr 2013 at 11:53

GoogleCodeExporter commented 9 years ago
Hi,

Unfortunately TinyJS doesn't come with a large library of standard functions, 
so quite a few of those type of things are missing.

If you want to, you could implement Array.push in TinyJS_Functions.cpp though - 
which would be useful for everyone - or you could just use out[out.length] = 
"Foo" instead.

Also, I think you could do the following in 42-tinyjs:
Array.prototype.push = function(x) { this[this.length] = x; }

In standard TinyJS, the prototype system is a bit broken so you'd end up doing:
Array.push = function(x) { this[this.length] = x; }

Original comment by pur3m...@googlemail.com on 11 Apr 2013 at 12:17

GoogleCodeExporter commented 9 years ago
Hi, thanks for your quick response.

I am working on SISR support for a speech recognition system. The system has 
come to use spidermonkey on windows, linux and iOS. For android I am planning 
to use tiny-js because I am having trouble in building spidermonkey, and for a 
mobile system I think tiny-js is a better fit.

For this system we have some scripts of our own and also expect the users to 
write some javascript code.

The Array push function exists in our own codes so javascript based solutions 
you offered are faster to develop and enough for me. Thank you.

After solving the Array.push this way now I am now having problem with 
__defineGetter__
ReferenceError: __defineGetter__ is undefined at Line:85 Column:46

for the following code:
meta.m_tsCurrentTextAndScore.__defineGetter__(\"score\", function() { return 
undefined; });

This was the mozilla specific way of adding a getter to a preexisting object. 
Do you have a suggestion for this?

Original comment by mertbuyu...@gmail.com on 11 Apr 2013 at 2:17

GoogleCodeExporter commented 9 years ago
I'm afraid you would need to modify TinyJS itself for that as I don't believe 
'getters' are implemented (they're not in normal TinyJS and it's unlikely in 
42-tinyjs). It shouldn't be a massive undertaking though as you'd just have to 
create a new flag that defines whether the field should be called or just 
returned directly.

Original comment by pur3m...@googlemail.com on 11 Apr 2013 at 2:40

GoogleCodeExporter commented 9 years ago
Hi,
After a day of working on this I've seen that the code for push should be like 
the following (for 42tiny-js):

Array.prototype.push = function(x) {
    var tmpCnt = this.length;
    this[tmpCnt] = x;
};

My web developer friends said "this" keyword is a dangerous one in Javascript. 
Using it twice in the same expression caused it not to work correctly.

About my second question,the getter, I've disabled the corresponding feature 
temporarily for now. 

The system works correctly under windows7 now.

In the android(using ndk-r8e) tests I've seen that my Javascript object to xml 
converter (namely NLSML generator) produces unexpected results.
The only difference I know is that 42tiny-js use boost regex for 
android(because <regex> header does not exist under android for now). I will 
work on this and try to post my findings when done.

Regards

Original comment by mertbuyu...@gmail.com on 22 Apr 2013 at 6:44

GoogleCodeExporter commented 9 years ago
Hello,
I've tried the system with boost regex(boost version: 1.53) on windows and it 
successfully worked. So it seems the problem is not with boost regex.
I've also tried the system both on windows and android without regex support. 
And observed the same success on windows and the same error on android. So I've 
eliminated the regex as the candidate for the source of my problems.

Note: It seems this issue has gone out of scope because the original question 
was just (Array.push - not defined?) so I will open a new issue with the 
explanation of my problem and close this one. 

Original comment by mertbuyu...@gmail.com on 22 Apr 2013 at 11:36

GoogleCodeExporter commented 9 years ago
Ok, thanks. To be honest I'm not sure I can be much help about you android 
compile problems - Standard TinyJS doesn't even have regexs, so I have no idea 
about 42-tinyjs. Could it just be an issue with character widths/multilanguage 
support though?

I should point out that we provide TinyJS for free for any use. Neither me nor 
Armin have made a single penny from it - and we can't afford to provide free 
tech support for anyone that uses it.

Original comment by pur3m...@googlemail.com on 22 Apr 2013 at 12:06