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

Commit

Permalink
[jsfm] add type check for register modules and components
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanks10100 committed Mar 13, 2019
1 parent bf88859 commit 16d0cf5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
3 changes: 2 additions & 1 deletion runtime/api/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

import { isPlainObject } from '../shared/utils'
import { registerElement } from '../vdom/WeexElement'

const weexComponents = {}
Expand All @@ -34,7 +35,7 @@ export function registerComponents (newComponents) {
if (typeof component === 'string') {
weexComponents[component] = true
}
else if (typeof component === 'object' && typeof component.type === 'string') {
else if (isPlainObject(component) && typeof component.type === 'string') {
weexComponents[component.type] = component
registerElement(component.type, component.methods)
}
Expand Down
24 changes: 14 additions & 10 deletions runtime/api/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@
* under the License.
*/

import { hasOwn, isPlainObject } from '../shared/utils'

const weexModules = {}

/**
* Register native modules information.
* @param {object} newModules
*/
export function registerModules (newModules) {
export function registerModules (newModules = {}) {
for (const name in newModules) {
if (!weexModules[name]) {
if (!hasOwn(weexModules, name)) {
weexModules[name] = {}
}
newModules[name].forEach(method => {
if (typeof method === 'string') {
weexModules[name][method] = true
}
else {
weexModules[name][method.name] = method.args
}
})
if (Array.isArray(newModules[name])) {
newModules[name].forEach(method => {
if (typeof method === 'string') {
weexModules[name][method] = true
}
else if (isPlainObject(method) && typeof method.name === 'string') {
weexModules[name][method.name] = method.args || []
}
})
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions runtime/frameworks/legacy/app/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ export function initModules (modules, ifReplace) {
}

// push each non-existed new method
modules[moduleName].forEach(function (method) {
if (typeof method === 'string') {
method = {
name: method
if (Array.isArray(modules[moduleName])) {
modules[moduleName].forEach(function (method) {
if (typeof method === 'string') {
method = {
name: method
}
}
}

if (!methods[method.name] || ifReplace) {
methods[method.name] = method
}
})
if (!methods[method.name] || ifReplace) {
methods[method.name] = method
}
})
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions runtime/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export function typof (v) {
return s.substring(8, s.length - 1)
}

export function isPlainObject (value) {
return Object.prototype.toString.call(value) === '[object Object]'
}

export function hasOwn (object, key) {
return isPlainObject(object) && Object.prototype.hasOwnProperty.call(object, key)
}

export function bufferToBase64 (buffer) {
if (typeof btoa !== 'function') {
return ''
Expand Down
2 changes: 1 addition & 1 deletion runtime/vdom/WeexElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const registeredElements = {}
*/
export function registerElement (type, methods) {
// Skip when no special component methods.
if (!methods || !methods.length) {
if (!Array.isArray(methods) || !methods.length) {
return
}

Expand Down

0 comments on commit 16d0cf5

Please sign in to comment.