-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#61): malloc + cage atoms and tests
- Loading branch information
1 parent
b6e4f9b
commit e72c23b
Showing
23 changed files
with
670 additions
and
81 deletions.
There are no files selected for viewing
21 changes: 12 additions & 9 deletions
21
eo2js-runtime/src/objects/org/eolang/cage$encaged$encage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 14 additions & 8 deletions
22
eo2js-runtime/src/objects/org/eolang/malloc$of$allocated$read.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 13 additions & 8 deletions
21
eo2js-runtime/src/objects/org/eolang/malloc$of$allocated$write.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const ErFailure = require('./error/ErFailure'); | ||
|
||
/** | ||
* Encaged objects. | ||
* @type {{}} | ||
*/ | ||
const OBJECTS = {} | ||
|
||
/** | ||
* Locators generator. | ||
* @type {number} | ||
*/ | ||
let locator = 0 | ||
|
||
/** | ||
* Cages for objects | ||
* @type {{ | ||
* init: function(Object): number, | ||
* encage: function(Number, Object), | ||
* get: function(Number): Object | ||
* }} | ||
*/ | ||
const cages = { | ||
/** | ||
* Encage object for the first time. | ||
* New locator will be generated. | ||
* @param {Number} object | ||
* @return {number} | ||
*/ | ||
init: function(object) { | ||
const loc = ++locator | ||
if (!OBJECTS.hasOwnProperty(loc)) { | ||
OBJECTS[loc] = object | ||
} | ||
return loc | ||
}, | ||
/** | ||
* Encage object to the storage by locator. | ||
* @param {Number} loc - Locator | ||
* @param {Object} object - Object | ||
*/ | ||
encage: function(loc, object) { | ||
if (!OBJECTS.hasOwnProperty(loc)) { | ||
throw new ErFailure( | ||
`Encaged object with locator ${loc} was not initialized, can't reencage, can't encage` | ||
) | ||
} | ||
const current = OBJECTS[loc].forma() | ||
const forma = object.forma() | ||
if (current !== forma) { | ||
throw new ErFailure( | ||
`Can't encage an object formed by ${forma} because object formed by ${current} was encaged before` | ||
) | ||
} | ||
OBJECTS[loc] = object | ||
}, | ||
/** | ||
* Retrieve object from storage by locator | ||
* @param {Number} loc - Locator | ||
* @return {Object} | ||
*/ | ||
get: function(loc) { | ||
if (!OBJECTS.hasOwnProperty(loc)) { | ||
throw new ErFailure( | ||
`Object with locator ${loc} is absent in cage, can't get` | ||
) | ||
} | ||
return OBJECTS[loc] | ||
} | ||
} | ||
|
||
module.exports = cages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.