refactor: rpc-server.js cleanup (#13420)
* refactor: don't declare variables C-style in JavaScript * refactor: use new constructor(...args) in rpc-server.js
This commit is contained in:
		
					parent
					
						
							
								44aad039c6
							
						
					
				
			
			
				commit
				
					
						08ccc2d624
					
				
			
		
					 1 changed files with 10 additions and 20 deletions
				
			
		| 
						 | 
					@ -171,7 +171,6 @@ const removeRemoteListenersAndLogWarning = (sender, meta, callIntoRenderer) => {
 | 
				
			||||||
// Convert array of meta data from renderer into array of real values.
 | 
					// Convert array of meta data from renderer into array of real values.
 | 
				
			||||||
const unwrapArgs = function (sender, args) {
 | 
					const unwrapArgs = function (sender, args) {
 | 
				
			||||||
  const metaToValue = function (meta) {
 | 
					  const metaToValue = function (meta) {
 | 
				
			||||||
    let i, len, member, ref, returnValue
 | 
					 | 
				
			||||||
    switch (meta.type) {
 | 
					    switch (meta.type) {
 | 
				
			||||||
      case 'value':
 | 
					      case 'value':
 | 
				
			||||||
        return meta.value
 | 
					        return meta.value
 | 
				
			||||||
| 
						 | 
					@ -191,15 +190,13 @@ const unwrapArgs = function (sender, args) {
 | 
				
			||||||
        let ret = {}
 | 
					        let ret = {}
 | 
				
			||||||
        Object.defineProperty(ret.constructor, 'name', { value: meta.name })
 | 
					        Object.defineProperty(ret.constructor, 'name', { value: meta.name })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ref = meta.members
 | 
					        for (const {name, value} of meta.members) {
 | 
				
			||||||
        for (i = 0, len = ref.length; i < len; i++) {
 | 
					          ret[name] = metaToValue(value)
 | 
				
			||||||
          member = ref[i]
 | 
					 | 
				
			||||||
          ret[member.name] = metaToValue(member.value)
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return ret
 | 
					        return ret
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      case 'function-with-return-value':
 | 
					      case 'function-with-return-value':
 | 
				
			||||||
        returnValue = metaToValue(meta.value)
 | 
					        const returnValue = metaToValue(meta.value)
 | 
				
			||||||
        return function () {
 | 
					        return function () {
 | 
				
			||||||
          return returnValue
 | 
					          return returnValue
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -237,9 +234,8 @@ const unwrapArgs = function (sender, args) {
 | 
				
			||||||
// Call a function and send reply asynchronously if it's a an asynchronous
 | 
					// Call a function and send reply asynchronously if it's a an asynchronous
 | 
				
			||||||
// style function and the caller didn't pass a callback.
 | 
					// style function and the caller didn't pass a callback.
 | 
				
			||||||
const callFunction = function (event, func, caller, args) {
 | 
					const callFunction = function (event, func, caller, args) {
 | 
				
			||||||
  let err, funcMarkedAsync, funcName, funcPassedCallback, ref, ret
 | 
					  const funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous')
 | 
				
			||||||
  funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous')
 | 
					  const funcPassedCallback = typeof args[args.length - 1] === 'function'
 | 
				
			||||||
  funcPassedCallback = typeof args[args.length - 1] === 'function'
 | 
					 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    if (funcMarkedAsync && !funcPassedCallback) {
 | 
					    if (funcMarkedAsync && !funcPassedCallback) {
 | 
				
			||||||
      args.push(function (ret) {
 | 
					      args.push(function (ret) {
 | 
				
			||||||
| 
						 | 
					@ -247,15 +243,15 @@ const callFunction = function (event, func, caller, args) {
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
      func.apply(caller, args)
 | 
					      func.apply(caller, args)
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      ret = func.apply(caller, args)
 | 
					      const ret = func.apply(caller, args)
 | 
				
			||||||
      event.returnValue = valueToMeta(event.sender, ret, true)
 | 
					      event.returnValue = valueToMeta(event.sender, ret, true)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } catch (error) {
 | 
					  } catch (error) {
 | 
				
			||||||
    // Catch functions thrown further down in function invocation and wrap
 | 
					    // Catch functions thrown further down in function invocation and wrap
 | 
				
			||||||
    // them with the function name so it's easier to trace things like
 | 
					    // them with the function name so it's easier to trace things like
 | 
				
			||||||
    // `Error processing argument -1.`
 | 
					    // `Error processing argument -1.`
 | 
				
			||||||
    funcName = ((ref = func.name) != null) ? ref : 'anonymous'
 | 
					    const funcName = func.name || 'anonymous'
 | 
				
			||||||
    err = new Error(`Could not call remote function '${funcName}'. Check that the function signature is correct. Underlying error: ${error.message}`)
 | 
					    const err = new Error(`Could not call remote function '${funcName}'. Check that the function signature is correct. Underlying error: ${error.message}`)
 | 
				
			||||||
    err.cause = error
 | 
					    err.cause = error
 | 
				
			||||||
    throw err
 | 
					    throw err
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -306,10 +302,7 @@ ipcMain.on('ELECTRON_BROWSER_CONSTRUCTOR', function (event, id, args) {
 | 
				
			||||||
      throwRPCError(`Cannot call constructor on missing remote object ${id}`)
 | 
					      throwRPCError(`Cannot call constructor on missing remote object ${id}`)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Call new with array of arguments.
 | 
					    event.returnValue = valueToMeta(event.sender, new constructor(...args))
 | 
				
			||||||
    // http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
 | 
					 | 
				
			||||||
    let obj = new (Function.prototype.bind.apply(constructor, [null].concat(args)))()
 | 
					 | 
				
			||||||
    event.returnValue = valueToMeta(event.sender, obj)
 | 
					 | 
				
			||||||
  } catch (error) {
 | 
					  } catch (error) {
 | 
				
			||||||
    event.returnValue = exceptionToMeta(event.sender, error)
 | 
					    event.returnValue = exceptionToMeta(event.sender, error)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -339,10 +332,7 @@ ipcMain.on('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, id, method, a
 | 
				
			||||||
      throwRPCError(`Cannot call constructor '${method}' on missing remote object ${id}`)
 | 
					      throwRPCError(`Cannot call constructor '${method}' on missing remote object ${id}`)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Call new with array of arguments.
 | 
					    event.returnValue = valueToMeta(event.sender, new object[method](...args))
 | 
				
			||||||
    let constructor = object[method]
 | 
					 | 
				
			||||||
    let obj = new (Function.prototype.bind.apply(constructor, [null].concat(args)))()
 | 
					 | 
				
			||||||
    event.returnValue = valueToMeta(event.sender, obj)
 | 
					 | 
				
			||||||
  } catch (error) {
 | 
					  } catch (error) {
 | 
				
			||||||
    event.returnValue = exceptionToMeta(event.sender, error)
 | 
					    event.returnValue = exceptionToMeta(event.sender, error)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue