# Intent to ship: Hashbang syntax

## Introduction

As JS metastasizes from the web to Node and shell scripts and the like, it 
becomes useful to run scripts directly in the shell and let the shell decide 
how.  The traditional way to do this is with an initial hashbang (or shebang) 
line indicating a path to an interpreter:

```
#!/usr/bin/env node
print("Hello world");
```

Thus many JS shells manually grunge past an initial "#!" line as convenience.  
SpiderMonkey's shell has done this since, well, before some people reading this 
were born.  :-)  https://dxr.mozilla.org/classic/source/js/src/js.c#143  But 
this means every implementation has micro-forked the language in perhaps subtly 
different manner.  (It's easy to mess up line numbers doing this.)

So it makes sense to standardize this as a micro-tax implementations will all 
pay consistently.  Hashbang isn't very valuable on the web -- polyglot web 
scripts/shell scripts are likely rare now -- but it's a minor nicety.

## How it works

If eval code, an entire script, or a module (but *not* the code string passed 
to the Function constructor, nor the contents of a block in normal JS source 
text) -- at its *very start* begins with the two code points U+0023 NUMBER SIGN 
followed by U+0021 EXCLAMATION MARK, all code points up to a LineTerminator are 
treated as a single-line comment -- exactly as if "//" were encountered instead 
of "#!".

Hashbang only begins a comment at the very start of a script; it's a syntax 
error elsewhere, including after any amount of whitespace.

A minor wrinkle: if you have an inline script element with the <script> on one 
line, then you have a linebreak and *then* your source text starts, that isn't 
necessarily the very start of the script data.  In XHTML it definitely *isn't* 
the start of the script -- "#!" would have to appear immediately after the ">" 
at end of the <script> element open.  But in plain old HTML *possibly* a single 
linebreak is ignored.  Memory hazy, too lazy to look up.

## Tracking bugs

https://bugzilla.mozilla.org/show_bug.cgi?id=hashbang

## Standard

https://github.com/tc39/proposal-hashbang

The standard is "stage 3" in TC39, the ECMAScript standardization committee: 
https://github.com/tc39/proposals#stage-3.

## Platform coverage

All

## Estimated or target release

67 (soft freeze 11 march, train leaving 18 march)

## MDN

...none yet, we'll find a place.  This feature is low-level enough the place to 
slot docs in is not immediately obvious.

## Support in other browser / JS engines

https://github.com/v8/v8/commit/0cd67eb7c57e109aa4cbc8b8bdff6b9eaf8c1286 
suggests v8 and Chrome are shipping this as of this week.

I couldn't find Safari/JavaScriptCore implementation discussion, but I didn't 
look much.

Node and other shells may already support something nigh-indistinguishable from 
this as an extension.

## test262 coverage

The relevant test262 tests will land in our tree shortly.  There aren't many, 
but there are enough.  (And honestly some of them verge on silliness: "what if 
the script starts with literally \u0023\u0021" is not a realistic 
implementation mistake. :-) )

https://github.com/tc39/test262/tree/master/test/language/comments/hashbang

## Where to send your bugs

https://bugzilla.mozilla.org/enter_bug.cgi?product=Core&component=JavaScript%20Engine
 and feel free to CC me.  :-)  But my good friend George Washington says you 
won't find any.  (What can I say?  I'm a high roller.)
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to