Skip to content

Commit

Permalink
Implements #167
Browse files Browse the repository at this point in the history
  • Loading branch information
iccir committed Mar 1, 2023
1 parent 4acf964 commit 07f422b
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 435 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ syntax consistency.
This is the first major release under the new language name (NilScript); as such,
runtime APIs and generated code use "ns"/"NS"/"nilscript" rather than "oj"/"OJ".

API Changes:
- All API functions are expected to use Promises (#167).

Major Language Changes:
- `@implementation` is now `@class` (#140).
- JavaScript functions and variables may no longer be used inside of a `@class` (#166).
Expand Down
50 changes: 15 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ This allows Number instance variables to be used in math operations without the

Unlike other parts of the NilScript runtime, properties and instance variables aren't intended to be accessed from non-NilScript JavaScript (they should be private to the subclass which defines them). However, they may need to be accessed in the debugger.

By default, the compiler uses a JavaScript property on the instance object with the follow name:
The compiler uses a JavaScript property on the instance object with an underscore prefix:

N$_i_{{IVAR NAME}}
_{{IVAR NAME}}


Hence, the following NilScript code:
Expand All @@ -387,35 +387,13 @@ nilscript.makeClass(…, function(…) {
… // Compiler generates -setCounter: and -counter here
….incrementCounter = function() {
this.N$_i__counter++;
}
});
```

### <a name="simple-ivars"></a>Simple Ivars

To make debugging easier, the `--simple-ivars` compiler flag may be used. This uses the exact ivar name as the JavaScript property.

Hence, the previous example would compile into:

```
nilscript.makeClass(…, function(…) {
… // Compiler generates -setCounter: and -counter here
….incrementCounter = function() {
this._counter++;
}
});
```

As long as ivars are prefixed (either the default `_theIvarName` or the non-standard prefixes such as `m_theIvarName` or `mTheIvarName`), this should be safe. You may run into issues if you use this flag and also use an ivar name which conflicts with an `Object.prototype` property. Examples include: `constructor`, `hasOwnProperty`, `toString`, `valueOf`, etc.

`--simple-ivars` has no effect when the squeezer is enabled.

---
## <a name="observers"></a>Property Observers

Expand Down Expand Up @@ -861,10 +839,12 @@ Traditionally, NilScript's API consisted of a single `compile` method:
```javascript
let nsc = require("nilscript");
let options = { … };

nsc.compile(options, function(err, results) {

});

async function doCompile() {
let results = await nsc.compile(options);

// Do something with results
}
```

To allow for fast incremental compiles, NilScript 2.x adds a `Compiler` constructor:
Expand All @@ -878,10 +858,10 @@ let compiler = new nsc.Compiler();
let options = { … };

// Call doCompile() each time one of the files specified by options.files changes
function doCompile(callback) {
compiler.compile(options, function(err, results) {
callback(err, results);
});
async function doCompile() {
let results = compiler.compile(options);

// Do something with results
}
```

Expand Down Expand Up @@ -944,7 +924,7 @@ The callback must return a Promise. When this callback is invoked, a file's cont

```javascript
// Simple preprocessor example. Strips out #pragma lines and logs to console
options["before-compile"] = async (file) => {
options["before-compile"] = async file => {
let inLines = file.getContents().split("\n");
let outLines = [ ];

Expand All @@ -964,7 +944,7 @@ options["before-compile"] = async (file) => {
};

// ESLint example
options["after-compile"] = async (file) => {
options["after-compile"] = async file => {
if (!linter) linter = require("eslint").linter;

// file.getContents() returns the generated source as a String
Expand All @@ -975,7 +955,7 @@ options["after-compile"] = async (file) => {
};

// Babel example
options["after-compile"] = async (file) => {
options["after-compile"] = async file => {
if (!babel) babel = require("babel-core");

// retainLines must be true or NilScript's output source map will be useless
Expand Down
10 changes: 9 additions & 1 deletion bin/nsc
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,16 @@ if (outputSymbolsFile) {
}


function compile(options, callback)
{
nilscript.compile(options)
.then(result => callback(null, result))
.catch(err => callback(err));
}


try {
nilscript.compile(options, function(err, result) {
compile(options, function(err, result) {
if (err) {
print_error(err);
process.exit(1);
Expand Down
41 changes: 4 additions & 37 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ const CompilerImpl = require("../src/Compiler");
const NSSymbolTyper = require("../src/model/NSSymbolTyper");


function _logInternalErrors(errors)
{
_.each(errors, error => {
if (error.name.indexOf("NilScript") !== 0) {
console.error("Internal NilScript error!")
console.error("------------------------------------------------------------");
console.error(error);
console.error(error.stack);
console.error("------------------------------------------------------------");
}
});
}


class Compiler {

constructor()
Expand All @@ -36,28 +22,9 @@ uses(compiler)
}


compile(options, callback)
async compile(options)
{
try {
this._impl.compile(options, (err, results) => {
if (err) {
if (!results) results = { errors: [ err ] };
if (!results.errors) results.errors = [ err ];
if (!results.errors.length) results.errors = [ err ];

} else if (!err && results.errors && results.errors.length) {
err = results.errors[0];
}

_logInternalErrors(results.errors);

callback(err, results);
});

} catch (err) {
_logInternalErrors([ err ]);
callback(err, { errors: [ err ] });
}
return this._impl.compile(options);
}


Expand All @@ -67,8 +34,8 @@ compile(options, callback)
module.exports = {
Compiler: Compiler,

compile: function(options, callback) {
(new Compiler()).compile(options, callback);
compile: async function(options) {
return (new Compiler()).compile(options);
},

getRuntimePath: function() {
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"bin": {
"nsc": "./bin/nsc"
},
"version": "2.1.0",
"engines": { "node": ">=6.0" },
"version": "3.0.0",
"engines": { "node": ">=18.0" },
"maintainers": [{
"name": "Ricci Adams",
"email": "[email protected]",
Expand All @@ -22,10 +22,8 @@
"mocha": "*"
},
"dependencies": {
"async": "1.x",
"estraverse": "4.x",
"lodash": "4.x",
"mkdirp": "0.5.x",
"node-getopt": "0.2.x",
"source-map": "0.5.x",
"typescript": "3.9.x"
Expand Down
Loading

0 comments on commit 07f422b

Please sign in to comment.