# Javascript + libC = Love!

#### xox

Joined Sep 8, 2017
496
Started out as just a little weekend project but now it's actually turning out to pretty useful. Still have a LOT more code to write (not to mention documentation). I wonder if it could be ported easily to a microcontroller?

FWIW here's the world's first truly hybrid C/Javascript program ever written:

JavaScript:
print("Sample Cosi Application")
print("(NOTE: default filename is 'main.js')")

function tick()
{
return clock() / CLOCKS_PER_SEC
}

function wait(delay)
{
var rest = tick() + delay
while(tick() < rest)
continue
}

function print_mechanical(pace, text)
{
srand(time(NULL))
if(pace > 1)
pace = 1 / pace

function put_mechanical(byte)
{
var modulated = pace * (rand() / RAND_MAX)
putchar(byte)
fflush(stdout)
wait(modulated)
}

var combined = "";
for(var idx = 1; idx < arguments.length; ++idx)
combined += arguments[idx]
var bytes = text_to_bytes(combined)
loop(combined, function(idx)
{
put_mechanical(get_byte(bytes, idx))
})
put_mechanical(0xa)
free(bytes)
}

var args = argv_to_text_array()
if(!args.length)
args = [1/4]
var text = file_to_text(script_path())
loop(args, function(idx)
{
var pace = Number(args[idx])
print("*** Script (printed with", pace, "second delays) ***")
print_mechanical(pace, text)
})

Last edited:

#### nsaspook

Joined Aug 27, 2009
7,733

#### xox

Joined Sep 8, 2017
496
Nice! But yes even a project like that could be coded almost entirely in Javascript. Speed might be an issue however in tight processing loops and what have you so some of it would probably still have to be written in C.

Is there any way you can try to write a small PIC program that uses Cosi?

#### nsaspook

Joined Aug 27, 2009
7,733
Javascript is the spawn of Satan in the embedded world. A necessary evil when writing code for browser apps execution but it's the wrong tool for small device programming.

#### xox

Joined Sep 8, 2017
496
Nah you just write the heavy stuff in C.

Thing about Javascript is that you really can write programs in a much more straightforward fashion than with C. Much simpler program logic, free memory management, and no compilation or linking required. Just copy a text file into your project or whatever and run.

I was just wondering because I know a PIC might not have all of the standard C functions present. Even so you could still just create the appropriate header file in the top-level Cosi directory with functions made to be either no-ops or, perhaps, actual filled out implementations of missing functions.

#### nsaspook

Joined Aug 27, 2009
7,733
Nah you just write the heavy stuff in C.

Thing about JavaScript is that you really can write programs in a much more straightforward fashion than with C. Much simpler program logic, free memory management, and no compilation or linking required. Just copy a text file into your project or whatever and run.
All of those things have a cost that on a resource limited micro-controller are potential project killers. C is very straightforward at the level of detail needed to optimize the types of problems commonly encountered on a controller using the 'procedural' programming paradigm for the 'heavy' stuff. Javascript has a very large place in the programming universe but it's not a good choice for bare-bones programming.

If something is possible, that doesn't mean it should necessarily be done. A JavaScript interpreter on anything less than a high end 32-bit controller IMO meets that criteria.

#### xox

Joined Sep 8, 2017
496
I don't think you really understand the domain of this type of tool very well. There's a good reason why much of our computing logic has been shifted to scripting languages over the past few years. As for myself I have personally been involved with multiple projects which for the most part consist of nothing but script. All of these products fall in the category of server software. Running right now, as we speak, just humming along, happily processing megabytes and megabytes of data...all the while earning me a few bucks in the process. And that is becoming the norm these days. What makes this possible is the concept of encapsulation. Tight routines are always written at the lowest level. Just about everything else gets moved to the scripting plane.

Old fogies such as yourself just don't like the idea of using anything less efficient than necessary. Hell it was the mantra back in the day. But times are changing now. You don't have to use slow hardware. These days you can go out and purchase a tiny 64-bit processor that requires very little power and more than sufficiently capable of running a program implemented (for the most part anyway) entirely in script.

#### xox

Joined Sep 8, 2017
496
Sounds like an amazing application. You must be sitting in a charming little Italian cafe right now sipping a mocha latte. Well I am anyway. I'm just saying that it's not as far fetched as you make it out to be. Familiar with the MIPS-6600? It consumes less than a single watt of power and offers impressive performance.

So yes you can do things many different ways. I was just thinking outside of the box for a bit as a little experiment and rather pleased with the result frankly. I just think it deserves a closer look. Maybe you don't see things that way. Fine but why shoot down someone else's efforts but for your own insecurities and ignorance?

#### nsaspook

Joined Aug 27, 2009
7,733
Sounds like an amazing application. You must be sitting in a charming little Italian cafe right now sipping a mocha latte. Well I am anyway. I'm just saying that it's not as far fetched as you make it out to be. Familiar with the MIPS-6600? It consumes less than a single watt of power and offers impressive performance.

So yes you can do things many different ways. I was just thinking outside of the box for a bit as a little experiment and rather pleased with the result frankly. I just think it deserves a closer look. Maybe you don't see things that way. Fine but why shoot down someone else's efforts but for your own insecurities and ignorance?
I'm not shooting down your project, I'm just giving you a reality check about micro-controllers and the economics of engineering products instead of just writing code like hardware resources are unlimited..

"If builders built houses the way programmers built programs, the first woodpecker to come along would destroy civilization."

#### spinnaker

Joined Oct 29, 2009
7,835
@xox is working hard to get on a lot of forum member's block list.

#### joeyd999

Joined Jun 6, 2011
4,433
Old fogies such as yourself just don't like the idea of using anything less efficient than necessary.
Ha! The new debate: .asm vs. Javascript.

Game on!

#### joeyd999

Joined Jun 6, 2011
4,433
Q. Which will get you to the grocery store faster: a bicycle or a SpaceX rocket?

#### xox

Joined Sep 8, 2017
496
Q. Which will get you to the grocery store faster: a bicycle or a SpaceX rocket?
Bicycle. By the time you finish donning your space helmet and preparing for launch I'd already be back home in slippers.

#### joeyd999

Joined Jun 6, 2011
4,433
Who said anything about helmets? And no prep necessary. He said fastest, and you chose a different time frame than I did. Since I’m the boss, you get fired for incompetence.

You’ve to learn a lot, padawan!
Actually, if that rocket accelerates to close the speed of light, we'll all have aged years by the time you get home.

#### xox

Joined Sep 8, 2017
496
Actually, if that rocket accelerates to close the speed of light, we'll all have aged years by the time you get home.
Except that you can never actually approach the speed of light per se. It's more like a moving goalpost. You can however try to approach it and that is of course how time dilation works. But even assuming unlimited propulsion it would take years at one G acceleration to achieve anything impressive time travel wise. At a certain point though it's interesting you do sort of hit a tipping point where the shear magnitude of time travelled becomes astronomical. But I digress.

#### joeyd999

Joined Jun 6, 2011
4,433
xox

#### xox

Joined Sep 8, 2017
496
I'm not shooting down your project, I'm just giving you a reality check about micro-controllers and the economics of engineering products instead of just writing code like hardware resources are unlimited..

"If builders built houses the way programmers built programs, the first woodpecker to come along would destroy civilization."
Well for all practical purposes they are getting that way. It applies to 32-bit arches as well anyhow. No reason why you couldn't run a mostly scripted program on those systems either. And you can buy super-cheap low-power ATMEGA's that would provide a fine platform for just that sort of thing. Suitable for anything from coffee makers to fully networked sensor based applications. Or whatever.

#### djsfantasi

Joined Apr 11, 2010
6,959
Troll

#### xox

Joined Sep 8, 2017
496
I wasn't trying to impress just sharing a fun project I had been working on really. But you seem to have a complex over that. Maybe you should go take a walk or something I don't know. You're mind is clouded with contempt for others obviously...

#### nsaspook

Joined Aug 27, 2009
7,733
Well for all practical purposes they are getting that way. It applies to 32-bit arches as well anyhow. No reason why you couldn't run a mostly scripted program on those systems either. And you can buy super-cheap low-power ATMEGA's that would provide a fine platform for just that sort of thing. Suitable for anything from coffee makers to fully networked sensor based applications. Or whatever.
Why is scripted so slick? The able programmer uses the tools that are best for the job at hand. They don't try to 'One-Trick Pony' one language into a universal jackhammer.