Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
add runtime safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8890 committed Jun 16, 2016
1 parent 502544f commit 4fd8ab2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog


### 1.0.9 (2016-06-16)
- Polish: add runtime safety checks for re-assigning bound objects and arrays.


### 1.0.8 (2016-06-16)
- Fix: edge case in array index setter which would not insert a new node if needed.

Expand Down
2 changes: 1 addition & 1 deletion benchmark/dbmonster/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</table>
</template>
<script src="ENV.js"></script>
<script src="../../dist/simulacra.js"></script>
<script src="http://simulacra.js.org/simulacra.min.js"></script>
<script src="http://localvoid.github.io/perf-monitor/0.1/perf-monitor.js"></script>
<script>
perfMonitor.startFPSMonitor()
Expand Down
14 changes: 12 additions & 2 deletions lib/bind_keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var processNodes = require('./process_nodes')
var keyMap = require('./key_map')

var hasBindingsKey = keyMap.hasBindings
var hasDefinitionKey = keyMap.hasDefinition
var isBoundToParentKey = keyMap.isBoundToParent
var hasMutatorsKey = keyMap.hasMutators
Expand Down Expand Up @@ -36,6 +37,11 @@ function bindKeys (scope, obj, def, parentNode, path) {
throw new TypeError(
'Invalid type of value "' + obj + '", object expected.')

if (obj[hasBindingsKey])
throw new Error('Can not re-assign bound object.')

Object.defineProperty(obj, hasBindingsKey, { value: true })

for (i = 0, j = defKeys.length; i < j; i++) bindKey(defKeys[i])

function bindKey (key) {
Expand Down Expand Up @@ -91,7 +97,10 @@ function bindKeys (scope, obj, def, parentNode, path) {
value = isArray ? x : [ x ]

// Assign custom mutator methods on the array instance.
if (isArray && !value[hasMutatorsKey]) {
if (isArray) {
if (value[hasMutatorsKey]) throw new Error(
'Can not re-assign bound array on field "' + key + '".')

Object.defineProperty(value, hasMutatorsKey, { value: true })

// These mutators preserve length.
Expand All @@ -108,7 +117,8 @@ function bindKeys (scope, obj, def, parentNode, path) {
value.splice = splice

// Handle array index assignment.
for (i = 0, j = value.length; i < j; i++) defineIndex(value, i)
for (i = 0, j = value.length; i < j; i++)
defineIndex(value, i)
}

// Handle rendering to the DOM. This algorithm tries to batch insertions
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function simulacra (obj, def) {
'The first position of top-level must be a Node or a CSS selector string.')

ensureNodes(this, def[0], def[1])
Object.defineProperty(obj, hasBindingsKey, { value: true })
node = processNodes(this, def[0].cloneNode(true), def[1])

path = []
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simulacra",
"description": "One-way data binding for web applications.",
"version": "1.0.8",
"version": "1.0.9",
"license": "MIT",
"author": {
"email": "0x8890@airmail.cc",
Expand Down Expand Up @@ -35,6 +35,7 @@
"domino": "^1.0.25",
"eslint": "^2.11.1",
"eslint-config-boss": "^1.0.3",
"fs-extra": "^0.30.0",
"highlight.js": "^9.4.0",
"html-minifier": "^2.1.3",
"istanbul": "^0.4.3",
Expand Down
11 changes: 10 additions & 1 deletion website/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const fs = require('fs')
const fs = require('fs-extra')
const path = require('path')
const domino = require('domino')
const simulacra = require('../lib')
Expand Down Expand Up @@ -99,6 +99,15 @@ postcss([ atImport, cssnext, cssnano() ])
fs.writeFileSync(path.join(outputPath, 'CNAME'), CNAME)


// Copy benchmarks folder
// ======================

fs.copySync(
path.join(__dirname, '../benchmark/dbmonster'),
path.join(outputPath, 'dbmonster'),
{ clobber: true })


// Done!
// =====

Expand Down

0 comments on commit 4fd8ab2

Please sign in to comment.