Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14896 > unrolled thread

Extending JavaScript (C lib)

Started bymathieu.malaterre@gmail.com
First post2012-07-06 00:32 -0700
Last post2012-07-09 11:37 +0200
Articles 9 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Extending JavaScript (C lib) mathieu.malaterre@gmail.com - 2012-07-06 00:32 -0700
    Re: Extending JavaScript (C lib) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-07-06 07:57 +0000
      Re: Extending JavaScript (C lib) "Scott Sauyet" <3103invalid@webnews.fastusenet.org> - 2012-07-06 12:05 +0000
        Re: Extending JavaScript (C lib) Stefan Weiss <krewecherl@gmail.com> - 2012-07-06 15:05 +0200
    Re: Extending JavaScript (C lib) "Scott Sauyet" <3103invalid@webnews.fastusenet.org> - 2012-07-06 12:31 +0000
      Re: Extending JavaScript (C lib) mathieu.malaterre@gmail.com - 2012-07-06 06:30 -0700
        Re: Extending JavaScript (C lib) "Scott Sauyet" <3103invalid@webnews.fastusenet.org> - 2012-07-06 14:36 +0000
        Re: Extending JavaScript (C lib) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-07-06 11:12 -0700
    Re: Extending JavaScript (C lib) Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-07-09 11:37 +0200

#14896 — Extending JavaScript (C lib)

Frommathieu.malaterre@gmail.com
Date2012-07-06 00:32 -0700
SubjectExtending JavaScript (C lib)
Message-ID<23cade81-201f-4f8c-92dc-110870559add@googlegroups.com>
Hi there,

  I am slowly discovering the JavaScript world, and I would like to know how I can extend JavaScript to use one of my custom C lib. I have seen dozens of different solutions (libffi, mosync, js-ctypes...) but I can't seem to find a solution that is:
- portable (across recent browsers),
- efficient in term of speed,
- safe (in term of coding)

libffi seems ok, but it requires the JavaScript programmer to know the ABI of the C function, so this does not scale well as new functions are added or old ones are changed...

Thanks much for comments !
-M

[toc] | [next] | [standalone]


#14898

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-07-06 07:57 +0000
Message-ID<XnsA088653FBA3EDeejj99@194.109.133.133>
In reply to#14896
 wrote on 06 jul 2012 in comp.lang.javascript:

> Hi there,
> 
>   I am slowly discovering the JavaScript world, and I would like to
>   know how I can extend JavaScript to use one of my custom C lib. I
>   have seen dozens of different solutions (libffi, mosync,
>   js-ctypes...) but I can't seem to find a solution that is: 
> - portable (across recent browsers),
> - efficient in term of speed,
> - safe (in term of coding)
> 
> libffi seems ok, but it requires the JavaScript programmer to know the
> ABI of the C function, so this does not scale well as new functions
> are added or old ones are changed... 

Mathieu,

[please us a sig on usenet]

That does not seem very practical.

Javascript being a loose-typed scripting language 
and C a strict-typed compiling one.

Javascript has to run on different engines and didfferent versions of those 
languages, all having diferent levels of implementations, with M$ being the 
main culprit of noncompliance.

So programming cannot be done with C, C+, C++ still in mind, as several 
recent additions to this NG have experienced.  

While it might be possible, it would be a buggy result.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#14903

From"Scott Sauyet" <3103invalid@webnews.fastusenet.org>
Date2012-07-06 12:05 +0000
Message-ID<201207061205233103@webnews.fastusenet.org>
In reply to#14898
Evertjan wrote:
> Mathieu wrote:
>> 
>>   I am slowly discovering the JavaScript world, and I would like to
>>   know how I can extend JavaScript to use one of my custom C lib. I
>>   have seen dozens of different solutions (libffi, mosync,
>>   js-ctypes...) but I can't seem to find a solution that is: 
>> - portable (across recent browsers),
>> - efficient in term of speed,
>> - safe (in term of coding)
>> 
>> libffi seems ok, but it requires the JavaScript programmer to know the
>> ABI of the C function, so this does not scale well as new functions
>> are added or old ones are changed... 
>
> [please us a sig on usenet]

Why?

> That does not seem very practical.
> 
> Javascript being a loose-typed scripting language 
> and C a strict-typed compiling one.
> 
> Javascript has to run on different engines and didfferent versions 
> of those languages, all having diferent levels of implementations,
> with M$ being the  main culprit of noncompliance.

I think you're crossing levels here.  Mathieu is talking about extensions 
to the language, which is usually implemented, I believe, in C.  I know 
that Rhino is a Java project, and there are other non-C implementations, 
and I'm really only guessing.  Can someone enlighten us definitively on 
the main implementations?

So to write an extension to the language, it would probably make the most 
sense to write it in the language used to implement the target language.  

For instance, Javascript has, as well as the very generic Object and Array 
constructors, much more specific Date and RegExp ones built in.  Perhaps 
Mathieu wants to do image processing and would really like a Color 
constructor built into the language, one that would be more efficient than 
something you might build in the language with red, green, and blue 
properties, or even with a three integer array.  That could be done, if he 
had access to the source code of the language by adding a new native Color 
constructor, written in C, and optimized beyond what might be available 
inside Javascript itself.

I will respond separately to the original post as to my thoughts on the 
feasibility of Mathieu's ideas, but I don't think the static nature of C 
versus the dynamic nature of Javascript is any hindrance.

  -- Scott

[toc] | [prev] | [next] | [standalone]


#14905

FromStefan Weiss <krewecherl@gmail.com>
Date2012-07-06 15:05 +0200
Message-ID<jt6nr2$9ni$1@news.albasani.net>
In reply to#14903
On 2012-07-06 14:05, Scott Sauyet wrote:
> I know that Rhino is a Java project, and there are other non-C
> implementations, and I'm really only guessing.  Can someone enlighten
> us definitively on the main implementations?

All scripting runtimes in the current major browsers are written in C++:

  SpiderMonkey (Firefox)
  V8 (Chrome)
  JScript (IE)
  SquirrelFish/JavaScriptCore (Webkit, Safari)
  Caracan? (Opera)

(The products in parentheses are only typical examples. V8, for example,
is used in various other software, like Node.js)

There are many other implementations in various languages. Probably the
most prominent one is Mozilla Rhino, written Java. An interesting
implementation is Narcissus (JavaScript engine in JavaScript), written
by Brendan Eich himself.


I can't help much with the original question; I've only ever extended
JavaScript in Java, using Rhino. I think the portability requirement is
going to be problematic. Depending on what the OP wants to do, there may
be other options. For example, Google Native Client (NaCl) can run
native code from the browser (not at all portable, of course).

- stefan

[toc] | [prev] | [next] | [standalone]


#14904

From"Scott Sauyet" <3103invalid@webnews.fastusenet.org>
Date2012-07-06 12:31 +0000
Message-ID<201207061231433103@webnews.fastusenet.org>
In reply to#14896
Mathieu Malaterre wrote:
> I am slowly discovering the JavaScript world, and I would like to
> know how I can extend JavaScript to use one of my custom C lib. I 
> have seen dozens of different solutions (libffi, mosync, 
> js-ctypes...) [ ... ]

I haven't done C in years, and never done very much with it, so my
answer might not be what you're looking for.

I don't think you're going to able to do this, though.  Not if you're
trying to run in a browser environment.  There are too many different
browsers, each with its proprietary implementation of ECMAScript.

Here is a case where I agree wholeheartedly with an admonition Thomas
Lahn often makes in this group.  There simply is no single "Javascript"
for you to extend.

If you ending working in a more homogeneous environment, then things
might be a bit different.  For instance, node, the server-side 
Javascript environment, runs inside Google's V8 engine.  You could
extend that, and use your extended version fairly successfully, I would
think.  But then you could probably only use your code under servers
under your direct control, where you could install your altered version
of node.

So what sort of extensions are you trying to make to the language?  Are 
there good reasons you can't implement the things you want in the 
language itself?

  -- Scott

[toc] | [prev] | [next] | [standalone]


#14906

Frommathieu.malaterre@gmail.com
Date2012-07-06 06:30 -0700
Message-ID<0b3de880-ccc5-408f-a29e-de6b0627f052@googlegroups.com>
In reply to#14904
On Friday, July 6, 2012 2:31:45 PM UTC+2, Scott Sauyet wrote:
> Mathieu Malaterre wrote:
> > I am slowly discovering the JavaScript world, and I would like to
> > know how I can extend JavaScript to use one of my custom C lib. I 
> > have seen dozens of different solutions (libffi, mosync, 
> > js-ctypes...) [ ... ]
> 
> I haven't done C in years, and never done very much with it, so my
> answer might not be what you're looking for.
> 
> I don't think you're going to able to do this, though.  Not if you're
> trying to run in a browser environment.  There are too many different
> browsers, each with its proprietary implementation of ECMAScript.
> 
> Here is a case where I agree wholeheartedly with an admonition Thomas
> Lahn often makes in this group.  There simply is no single "Javascript"
> for you to extend.
> 
> If you ending working in a more homogeneous environment, then things
> might be a bit different.  For instance, node, the server-side 
> Javascript environment, runs inside Google's V8 engine.  You could
> extend that, and use your extended version fairly successfully, I would
> think.  But then you could probably only use your code under servers
> under your direct control, where you could install your altered version
> of node.
> 
> So what sort of extensions are you trying to make to the language?  Are 
> there good reasons you can't implement the things you want in the 
> language itself?

I am sending compressed images to the client. I would like to (re)use the carefully coded & heavily optimized C lib to decompress the image on the client side.
I could limit this behavior to only one JavaScript implementation, the other implementations would receive PNG (JPG?) files and be noticed of the potential slow down.

Thanks.

[toc] | [prev] | [next] | [standalone]


#14907

From"Scott Sauyet" <3103invalid@webnews.fastusenet.org>
Date2012-07-06 14:36 +0000
Message-ID<201207061436533103@webnews.fastusenet.org>
In reply to#14906
> Mathieu Malaterre wrote:
> Scott Sauyet wrote:

>> So what sort of extensions are you trying to make to the language?  
>> Are there good reasons you can't implement the things you want in the 
>> language itself?
> 
> I am sending compressed images to the client. I would like to (re)use 
> the carefully coded & heavily optimized C lib to decompress the image 
> on the client side.

Any thoughts on how you would get your extensions installed on client
machines?  I don't know if any of them except for some experimental
Chrome versions have extension points that would allow you to load your
extensions dynamically.


> I could limit this behavior to only one JavaScript implementation, the
> other implementations would receive PNG (JPG?) files and be noticed of
> the potential slow down.

How complex is the decompression algorithm?  Javascript, especially in
newer implementations, can be surprisingly quick, although mathematical
algorithms are by no means its forte.

  -- Scott

[toc] | [prev] | [next] | [standalone]


#14913

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2012-07-06 11:12 -0700
Message-ID<6931111e-523b-4e6c-8d13-b8cde5cb2a91@tu6g2000pbc.googlegroups.com>
In reply to#14906
On Jul 6, 8:30 am, mathieu.malate...@gmail.com wrote:
> On Friday, July 6, 2012 2:31:45 PM UTC+2, Scott Sauyet wrote:
> > Mathieu Malaterre wrote:
> > > I am slowly discovering the JavaScript world, and I would like to
> > > know how I can extend JavaScript to use one of my custom C lib. I
> > > have seen dozens of different solutions (libffi, mosync,
> > > js-ctypes...) [ ... ]
>
> > I haven't done C in years, and never done very much with it, so my
> > answer might not be what you're looking for.
>
> > I don't think you're going to able to do this, though.  Not if you're
> > trying to run in a browser environment.  There are too many different
> > browsers, each with its proprietary implementation of ECMAScript.
>
> > Here is a case where I agree wholeheartedly with an admonition Thomas
> > Lahn often makes in this group.  There simply is no single "Javascript"
> > for you to extend.
>
> > If you ending working in a more homogeneous environment, then things
> > might be a bit different.  For instance, node, the server-side
> > Javascript environment, runs inside Google's V8 engine.  You could
> > extend that, and use your extended version fairly successfully, I would
> > think.  But then you could probably only use your code under servers
> > under your direct control, where you could install your altered version
> > of node.
>
> > So what sort of extensions are you trying to make to the language?  Are
> > there good reasons you can't implement the things you want in the
> > language itself?
>
> I am sending compressed images to the client. I would like to (re)use the carefully coded & heavily optimized C lib to decompress the image on the client side.
> I could limit this behavior to only one JavaScript implementation, the other implementations would receive PNG (JPG?) files and be noticed of the potential slow down.
>
> Thanks.

Have you seen emscripten yet? <https://github.com/kripken/emscripten>

[toc] | [prev] | [next] | [standalone]


#14967

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-07-09 11:37 +0200
Message-ID<4ffaa66c$0$6845$e4fe514c@news2.news.xs4all.nl>
In reply to#14896
On 7/6/2012 9:32 AM, mathieu.malaterre@gmail.com wrote:
> Hi there,
>
>    I am slowly discovering the JavaScript world, and I would like to know how I can extend JavaScript to use one of my custom C lib. I have seen dozens of different solutions (libffi, mosync, js-ctypes...) but I can't seem to find a solution that is:
> - portable (across recent browsers),
> - efficient in term of speed,
> - safe (in term of coding)
>
> libffi seems ok, but it requires the JavaScript programmer to know the ABI of the C function, so this does not scale well as new functions are added or old ones are changed...
>
> Thanks much for comments !
> -M
>


Can somebody explain to this simple soul how such extensions work?
ISTM that the extended Javascript will only work if you also distribute 
the (modified Javascript engine of the) browser yourself, *or* you ask 
your users to activate the software somehow (Something like greasemonkey?).
And how are new versions of the browser handled?

Regards,
Erwin Moller



-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web