Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/operations/ecma-function-object.c (C)

Code before:
#else /* !ENABLED (JERRY_SNAPSHOT_EXEC) */
  return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t, realm_value);
#endif /* ENABLED (JERRY_SNAPSHOT_EXEC) */
} /* ecma_op_function_get_realm */

/**
 * Get realm from a function
 *
 * Note:
 *   Does not increase the reference counter.
 *
 * @return realm (global) object
 */
ecma_global_object_t *
ecma_op_function_get_function_realm (ecma_object_t *func_obj_p) /**< function object */
{
  while (true)
  {
    if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
    {
      ecma_extended_object_t *ext_function_obj_p = (ecma_extended_object_t *) func_obj_p;
      const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_function_obj_p);
      return ecma_op_function_get_realm (bytecode_data_p);
    }

    if (ecma_get_object_is_builtin (func_obj_p))
    {
      ecma_extended_object_t *ext_function_obj_p = (ecma_extended_object_t *) func_obj_p;
      return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t,
                                              ext_function_obj_p->u.built_in.realm_value);
    }

    if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_NATIVE_FUNCTION)
    {
      ecma_native_function_t *native_function_p = (ecma_native_function_t *) func_obj_p;
      return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t,
                                              native_function_p->realm_value);
    }

  #if ENABLED (JERRY_BUILTIN_PROXY)
    if (ECMA_OBJECT_IS_PROXY (func_obj_p))
    {
      ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) func_obj_p;
      if (ecma_is_value_null (proxy_obj_p->handler))
      {
        ecma_raise_type_error (ECMA_ERR_MSG ("Prototype from revoked Proxy is invalid."));
        return NULL;
      }
      func_obj_p = ecma_get_object_from_value (proxy_obj_p->target);
      continue;
    }

Review comment (bug):
This check must be inside `if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_NATIVE_FUNCTION)`. It is strange there was no crash.

Code after:
#else /* !ENABLED (JERRY_SNAPSHOT_EXEC) */
  return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t, realm_value);
#endif /* ENABLED (JERRY_SNAPSHOT_EXEC) */
} /* ecma_op_function_get_realm */

/**
 * Get realm from a function
 *
 * Note:
 *   Does not increase the reference counter.
 *
 * @return realm (global) object
 */
ecma_global_object_t *
ecma_op_function_get_function_realm (ecma_object_t *func_obj_p) /**< function object */
{
  while (true)
  {
    if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
    {
      ecma_extended_object_t *ext_function_obj_p = (ecma_extended_object_t *) func_obj_p;
      const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_function_obj_p);
      return ecma_op_function_get_realm (bytecode_data_p);
    }

    if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_NATIVE_FUNCTION)
    {
      if (ecma_get_object_is_builtin (func_obj_p))
      {
        ecma_extended_object_t *ext_function_obj_p = (ecma_extended_object_t *) func_obj_p;
        return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t,
                                                ext_function_obj_p->u.built_in.realm_value);
      }
      ecma_native_function_t *native_function_p = (ecma_native_function_t *) func_obj_p;
      return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t,
                                              native_function_p->realm_value);
    }

  #if ENABLED (JERRY_BUILTIN_PROXY)
    if (ECMA_OBJECT_IS_PROXY (func_obj_p))
    {
      ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) func_obj_p;
      if (ecma_is_value_null (proxy_obj_p->handler))
      {
        ecma_raise_type_error (ECMA_ERR_MSG ("Prototype from revoked Proxy is invalid."));
        return NULL;
      }
      func_obj_p = ecma_get_object_from_value (proxy_obj_p->target);
      continue;
    }
  #endif /* ENABLED (JERRY_BUILTIN_PROXY) */