Skip to content

Commit 8ebd323

Browse files
galpeterrerobika
authored andcommitted
Make sure that snapshot API return values are the same as in the docs (#2997)
The snapshot API docs describe that those functions returns error if the related features are not enabled. Updated the return values to follow the API documentation. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent 1409a68 commit 8ebd323

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

docs/02.API-REFERENCE.md

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6505,8 +6505,12 @@ main (void)
65056505

65066506
Generate snapshot from the specified source code.
65076507

6508-
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6509-
is no longer needed.
6508+
*Notes*:
6509+
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6510+
is no longer needed.
6511+
- This API depends on a build option (`JERRY_SNAPSHOT_SAVE`) and can be checked in runtime with
6512+
the `JERRY_FEATURE_SNAPSHOT_SAVE` feature enum value, see [jerry_is_feature_enabled](#jerry_is_feature_enabled).
6513+
If the feature is not enabled the function will return an error.
65106514

65116515
**Prototype**
65126516

@@ -6560,7 +6564,11 @@ main (void)
65606564
global_mode_snapshot_buffer,
65616565
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
65626566

6563-
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
6567+
if (!jerry_value_is_error (generate_result))
6568+
{
6569+
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
6570+
}
6571+
65646572
jerry_release_value (generate_result);
65656573

65666574
jerry_cleanup ();
@@ -6586,8 +6594,12 @@ with the given arguments.
65866594
The function arguments and function body are
65876595
passed as separated arguments.
65886596

6589-
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6590-
is no longer needed.
6597+
*Notes*:
6598+
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6599+
is no longer needed.
6600+
- This API depends on a build option (`JERRY_SNAPSHOT_SAVE`) and can be checked in runtime with
6601+
the `JERRY_FEATURE_SNAPSHOT_SAVE` feature enum value, see [jerry_is_feature_enabled](#jerry_is_feature_enabled).
6602+
If the feature is not enabled the function will return an error.
65916603

65926604
**Prototype**
65936605

@@ -6648,7 +6660,11 @@ main (void)
66486660
func_snapshot_buffer,
66496661
sizeof (func_snapshot_buffer) / sizeof (uint32_t));
66506662

6651-
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
6663+
if (!jerry_value_is_error (generate_result))
6664+
{
6665+
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
6666+
}
6667+
66526668
jerry_release_value (generate_result);
66536669

66546670
jerry_cleanup ();
@@ -6670,8 +6686,12 @@ main (void)
66706686

66716687
Execute snapshot from the specified buffer.
66726688

6673-
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6674-
is no longer needed.
6689+
*Notes*:
6690+
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6691+
is no longer needed.
6692+
- This API depends on a build option (`JERRY_SNAPSHOT_EXEC`) and can be checked in runtime with
6693+
the `JERRY_FEATURE_SNAPSHOT_EXEC` feature enum value, see [jerry_is_feature_enabled](#jerry_is_feature_enabled).
6694+
If the feature is not enabled the function will return an error.
66756695

66766696
**Prototype**
66776697

@@ -6683,13 +6703,13 @@ jerry_exec_snapshot (const uint32_t *snapshot_p,
66836703
uint32_t exec_snapshot_opts);
66846704
```
66856705

6686-
- `snapshot_p` - pointer to snapshot
6687-
- `snapshot_size` - size of snapshot in bytes
6688-
- `func_index` - index of executed function
6706+
- `snapshot_p` - pointer to snapshot.
6707+
- `snapshot_size` - size of snapshot in bytes.
6708+
- `func_index` - index of executed function.
66896709
- `exec_snapshot_opts` - any combination of [jerry_exec_snapshot_opts_t](#jerry_exec_snapshot_opts_t) flags.
66906710
- return value
6691-
- result of bytecode, if run was successful
6692-
- thrown error, otherwise
6711+
- result of bytecode, if run was successful.
6712+
- thrown error, otherwise (an error is reported if the snapshot execution feature is not enabled).
66936713

66946714
*Changed in version 2.0*: Added `func_index` and `exec_snapshot_opts` arguments. Removed the `copy_bytecode` last argument.
66956715

@@ -6716,6 +6736,7 @@ main (void)
67166736
0,
67176737
global_mode_snapshot_buffer,
67186738
sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
6739+
// generate_result should be checked if it is an error or not
67196740

67206741
size_t global_mode_snapshot_size = (size_t) jerry_get_number_value (generate_result);
67216742
jerry_release_value (generate_result);
@@ -6728,6 +6749,9 @@ main (void)
67286749
global_mode_snapshot_size,
67296750
0,
67306751
0);
6752+
6753+
// check the `res` value for error and process the result.
6754+
67316755
jerry_release_value (res);
67326756

67336757
jerry_cleanup ();
@@ -6750,8 +6774,12 @@ Load the selected snapshot function from the specified buffer as a function obje
67506774

67516775
The lexical environment of the loaded function is always the global lexical environment.
67526776

6753-
*Note*: Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6754-
is no longer needed.
6777+
*Notes*:
6778+
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
6779+
is no longer needed.
6780+
- This API depends on a build option (`JERRY_SNAPSHOT_EXEC`) and can be checked in runtime with
6781+
the `JERRY_FEATURE_SNAPSHOT_EXEC` feature enum value, see [jerry_is_feature_enabled](#jerry_is_feature_enabled).
6782+
If the feature is not enabled the function will return an error.
67556783

67566784
**Prototype**
67576785

@@ -6846,6 +6874,11 @@ main (void)
68466874
Collect the used literals from the given snapshot and save them into a buffer in list or C format.
68476875
None of these literals are magic strings. In C format only valid identifiers are collected.
68486876

6877+
*Note*:
6878+
- This API depends on a build option (`JERRY_SNAPSHOT_SAVE`) and can be checked in runtime with
6879+
the `JERRY_FEATURE_SNAPSHOT_SAVE` feature enum value, see [jerry_is_feature_enabled](#jerry_is_feature_enabled).
6880+
If the feature is not enabled the function will return zero.
6881+
68496882
**Prototype**
68506883

68516884
```c

jerry-core/api/jerry-snapshot.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ jerry_generate_snapshot (const jerry_char_t *resource_name_p, /**< script resour
881881
JERRY_UNUSED (buffer_p);
882882
JERRY_UNUSED (buffer_size);
883883

884-
return 0;
884+
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot save is not supported.");
885885
#endif /* ENABLED (JERRY_SNAPSHOT_SAVE) */
886886
} /* jerry_generate_snapshot */
887887

@@ -1032,7 +1032,7 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
10321032
JERRY_UNUSED (func_index);
10331033
JERRY_UNUSED (exec_snapshot_opts);
10341034

1035-
return ECMA_VALUE_FALSE;
1035+
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot execution is not supported.");
10361036
#endif /* ENABLED (JERRY_SNAPSHOT_EXEC) */
10371037
} /* jerry_exec_snapshot */
10381038

@@ -1831,7 +1831,7 @@ jerry_generate_function_snapshot (const jerry_char_t *resource_name_p, /**< scri
18311831
JERRY_UNUSED (buffer_p);
18321832
JERRY_UNUSED (buffer_size);
18331833

1834-
return 0;
1834+
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot save is not supported.");
18351835
#endif /* ENABLED (JERRY_SNAPSHOT_SAVE) */
18361836
} /* jerry_generate_function_snapshot */
18371837

@@ -1858,6 +1858,6 @@ jerry_load_function_snapshot (const uint32_t *function_snapshot_p, /**< snapshot
18581858
JERRY_UNUSED (func_index);
18591859
JERRY_UNUSED (exec_snapshot_opts);
18601860

1861-
return ECMA_VALUE_FALSE;
1861+
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot execution is not supported.");
18621862
#endif /* ENABLED (JERRY_SNAPSHOT_EXEC) */
18631863
} /* jerry_load_function_snapshot */

0 commit comments

Comments
 (0)