以下参考jerryscript项目中关于 ecma 的实现:
typeof是直接判断值类型(位于jerry-core/vm/opcodes.c):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/**
* 'typeof' opcode handler.
*
* See also: ECMA-262 v5, 11.4.3
*
* @return ecma value
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
opfunc_typeof (ecma_value_t left_value) /**< left value */
{
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
ecma_string_t *type_str_p = NULL;
if (ecma_is_value_undefined (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_UNDEFINED);
}
else if (ecma_is_value_null (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_OBJECT);
}
else if (ecma_is_value_boolean (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BOOLEAN);
}
else if (ecma_is_value_number (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NUMBER);
}
else if (ecma_is_value_string (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING);
}
else
{
JERRY_ASSERT (ecma_is_value_object (left_value));
if (ecma_op_is_callable (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_FUNCTION);
}
else
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_OBJECT);
}
}
ret_value = ecma_make_string_value (type_str_p);
return ret_value;
} /* opfunc_typeof */
而Object.prototype.toString是泛型方法,只判断类类型(位于jerry-core/emca/builtin-objects/ecma-builtin-object-prototype.c):1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* The Object.prototype object's 'toString' routine
*
* See also:
* ECMA-262 v5, 15.2.4.2
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_object_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
{
return ecma_builtin_helper_object_to_string (this_arg);
} /* ecma_builtin_object_prototype_object_to_string */
1 | /** \addtogroup ecma ECMA |
1 | lit_magic_string_id_t |
对于其它的具体类型,仍然是判断值,例如: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/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup booleanprototype ECMA Boolean.prototype object built-in
* @{
*/
/**
* The Boolean.prototype object's 'toString' routine
*
* See also:
* ECMA-262 v5, 15.6.4.2
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_boolean_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
{
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
ECMA_TRY_CATCH (value_of_ret,
ecma_builtin_boolean_prototype_object_value_of (this_arg),
ret_value);
ecma_string_t *ret_str_p;
if (ecma_is_value_true (value_of_ret))
{
ret_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_TRUE);
}
else
{
JERRY_ASSERT (ecma_is_value_boolean (value_of_ret));
ret_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_FALSE);
}
ret_value = ecma_make_string_value (ret_str_p);
ECMA_FINALIZE (value_of_ret);
return ret_value;
} /* ecma_builtin_boolean_prototype_object_to_string */