all files / src/ext/ errors.js

64.29% Statements 36/56
0% Branches 0/14
33.33% Functions 1/3
64.29% Lines 36/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107            1×   1×     1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1×     1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1×     1×                                                             1× 1×                                                        
/*!
 * Copyright (C) 2017 Glayzzle (BSD3 License)
 * @authors https://github.com/glayzzle/php-transpiler/graphs/contributors
 * @url http://glayzzle.com
 */
'use strict';
module.exports = function($php) {
 
  var phpArray = require('../array');
 
  // defines a list of message types
  const E_ERROR             = $php.constant('E_ERROR');
  const E_WARNING           = $php.constant('E_WARNING');
  const E_PARSE             = $php.constant('E_PARSE');
  const E_NOTICE            = $php.constant('E_NOTICE');
  const E_CORE_ERROR        = $php.constant('E_CORE_ERROR');
  const E_CORE_WARNING      = $php.constant('E_CORE_WARNING');
  const E_COMPILE_ERROR     = $php.constant('E_COMPILE_ERROR');
  const E_COMPILE_WARNING   = $php.constant('E_COMPILE_WARNING');
  const E_USER_ERROR        = $php.constant('E_USER_ERROR');
  const E_USER_WARNING      = $php.constant('E_USER_WARNING');
  const E_USER_NOTICE       = $php.constant('E_USER_NOTICE');
  const E_STRICT            = $php.constant('E_STRICT');
  const E_RECOVERABLE_ERROR = $php.constant('E_RECOVERABLE_ERROR');
  const E_DEPRECATED        = $php.constant('E_DEPRECATED');
  const E_USER_DEPRECATED   = $php.constant('E_USER_DEPRECATED');
 
  // defines the message errors
  let errorTypes = {};
  errorTypes[E_ERROR] = "Error";
  errorTypes[E_WARNING] = "Warning";
  errorTypes[E_PARSE] = "Parsing Error";
  errorTypes[E_NOTICE] = "Notice";
  errorTypes[E_CORE_ERROR] = "Core Error";
  errorTypes[E_CORE_WARNING] = "Core Warning";
  errorTypes[E_COMPILE_ERROR] = "Compile Error";
  errorTypes[E_COMPILE_WARNING] = "Compile Warning";
  errorTypes[E_USER_ERROR] = "User Error";
  errorTypes[E_USER_WARNING] = "User Warning";
  errorTypes[E_USER_NOTICE] = "User Notice";
  errorTypes[E_STRICT] = "Strict Notice";
  errorTypes[E_RECOVERABLE_ERROR] = "Recoverable fatal error";
  errorTypes[E_DEPRECATED] = "Deprecated";
  errorTypes[E_USER_DEPRECATED] = "Deprecated";
 
  // http://php.net/manual/fr/function.trigger-error.php
  $php.context.function.declare(
    '\\trigger_error', [
      {
        type: 'string',
        name: 'error_msg'
      },
      {
        type: 'int',
        name: 'error_type'
      }
    ],
    'bool', function trigger_error(error_msg, error_type) {
      if (typeof error_type === 'undefined') {
        error_type = E_USER_ERROR;
      }
      var msg = errorTypes[error_type];
      if (!msg) {
        return false;
      }
      // @fixme handle filename and line number from stack
      var errMsg = 'PHP ' + msg + ': ' + error_msg + ' in X on line X';
      // @fixme error_handler
      // @fixme handle errors output configuration
      // @todo handling error state
      $php.stderr.print(errMsg);
      // @todo error logger
      return true;
    }
  );
 
  /** register global functions **/
  $php.trigger_error = $php.context.function.callback('\\trigger_error');
  $php.type_error = function(argPos, argName, fnName, argType, value) {
    var curType = 'mixed';
    if (value instanceof Number) {
      curType = 'int';
    } else if (typeof value === 'string') {
      curType = 'string';
    } else if (typeof value === 'boolean') {
      curType = 'bool';
    } else if (value instanceof phpArray) {
      curType = 'array';
    }
    if (argPos === -1) {
      // @fixme trigger an TypeError exception
      $php.trigger_error(
        'Return value of ' + fnName + '() ' +
        'must be of the type ' + argType + ', ' + curType + ' given',
        E_ERROR
      );
    } else {
      $php.trigger_error(
        'Argument ' + argPos + ' passed to ' + fnName + '() ' +
        'must be of the type ' + argType + ', ' + curType + ' given',
        E_ERROR
      );
    }
  };
 
};