Repository: jerryscript-project/jerryscript
File: jerry-core/api/jerry-snapshot.c (C)

Code before:
                         uint32_t *buffer_p, /**< buffer to save snapshot to */
                         size_t buffer_size) /**< the buffer's size */
{
#if JERRY_SNAPSHOT_SAVE
  return jerry_generate_snapshot_with_args (source_p,
                                            source_size,
                                            NULL,
                                            0,
                                            options_p,
                                            generate_snapshot_opts,
                                            buffer_p,
                                            buffer_size);
#else /* !JERRY_SNAPSHOT_SAVE */
  JERRY_UNUSED (source_p);
  JERRY_UNUSED (source_size);
  JERRY_UNUSED (options_p);
  JERRY_UNUSED (generate_snapshot_opts);
  JERRY_UNUSED (buffer_p);
  JERRY_UNUSED (buffer_size);

  return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot save is not supported");
#endif /* JERRY_SNAPSHOT_SAVE */
} /* jerry_generate_snapshot */

/**
 * Execute snapshot from specified buffer
 *
 * Note:
 *      returned value must be freed with jerry_release_value, when it is no longer needed.
 *
 * @return result of bytecode - if run was successful
 *         thrown error - otherwise
 */
jerry_value_t
jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
                     size_t snapshot_size, /**< size of snapshot */
                     size_t func_index, /**< index of primary function */
                     uint32_t exec_snapshot_opts) /**< jerry_exec_snapshot_opts_t option bits */
{
#if JERRY_SNAPSHOT_EXEC
  JERRY_ASSERT (snapshot_p != NULL);

  uint32_t allowed_opts = (JERRY_SNAPSHOT_EXEC_COPY_DATA
                           | JERRY_SNAPSHOT_EXEC_ALLOW_STATIC
                           | JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);

  if ((exec_snapshot_opts & ~(allowed_opts)) != 0)
  {
    ecma_raise_range_error (ECMA_ERR_MSG ("Unsupported snapshot exec flags are specified"));
    return ecma_create_error_reference_from_context ();
  }

Review comment (suggestion):
With the new flag this will load the function, and will return a function object (if I understand correctly). So removing the load seems weird. Maybe the whole method description should be reworded. Maybe: "Executes a snapshot from as specified buffer or if <flag> is used this will load/return it as a function object.".

Code after:
                         uint32_t *buffer_p, /**< buffer to save snapshot to */
                         size_t buffer_size) /**< the buffer's size */
{
#if JERRY_SNAPSHOT_SAVE
  return jerry_generate_snapshot_with_args (source_p,
                                            source_size,
                                            NULL,
                                            0,
                                            options_p,
                                            generate_snapshot_opts,
                                            buffer_p,
                                            buffer_size);
#else /* !JERRY_SNAPSHOT_SAVE */
  JERRY_UNUSED (source_p);
  JERRY_UNUSED (source_size);
  JERRY_UNUSED (options_p);
  JERRY_UNUSED (generate_snapshot_opts);
  JERRY_UNUSED (buffer_p);
  JERRY_UNUSED (buffer_size);

  return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot save is not supported");
#endif /* JERRY_SNAPSHOT_SAVE */
} /* jerry_generate_snapshot */

/**
 * Execute/load snapshot from specified buffer
 *
 * Note:
 *      returned value must be freed with jerry_release_value, when it is no longer needed.
 *
 * @return result of bytecode - if run was successful
 *         thrown error - otherwise
 */
jerry_value_t
jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
                     size_t snapshot_size, /**< size of snapshot */
                     size_t func_index, /**< index of primary function */
                     uint32_t exec_snapshot_opts) /**< jerry_exec_snapshot_opts_t option bits */
{
#if JERRY_SNAPSHOT_EXEC
  JERRY_ASSERT (snapshot_p != NULL);

  uint32_t allowed_opts = (JERRY_SNAPSHOT_EXEC_COPY_DATA
                           | JERRY_SNAPSHOT_EXEC_ALLOW_STATIC
                           | JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);

  if ((exec_snapshot_opts & ~(allowed_opts)) != 0)
  {
    ecma_raise_range_error (ECMA_ERR_MSG ("Unsupported snapshot exec flags are specified"));
    return ecma_create_error_reference_from_context ();
  }