11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/snapshot/context-deserializer.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/api/api-inl.h"
81cb0ef41Sopenharmony_ci#include "src/common/assert-scope.h"
91cb0ef41Sopenharmony_ci#include "src/heap/heap-inl.h"
101cb0ef41Sopenharmony_ci#include "src/objects/js-array-buffer-inl.h"
111cb0ef41Sopenharmony_ci#include "src/objects/slots.h"
121cb0ef41Sopenharmony_ci#include "src/snapshot/snapshot.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace internal {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciMaybeHandle<Context> ContextDeserializer::DeserializeContext(
181cb0ef41Sopenharmony_ci    Isolate* isolate, const SnapshotData* data, bool can_rehash,
191cb0ef41Sopenharmony_ci    Handle<JSGlobalProxy> global_proxy,
201cb0ef41Sopenharmony_ci    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
211cb0ef41Sopenharmony_ci  ContextDeserializer d(isolate, data, can_rehash);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  MaybeHandle<Object> maybe_result =
241cb0ef41Sopenharmony_ci      d.Deserialize(isolate, global_proxy, embedder_fields_deserializer);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  Handle<Object> result;
271cb0ef41Sopenharmony_ci  return maybe_result.ToHandle(&result) ? Handle<Context>::cast(result)
281cb0ef41Sopenharmony_ci                                        : MaybeHandle<Context>();
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciMaybeHandle<Object> ContextDeserializer::Deserialize(
321cb0ef41Sopenharmony_ci    Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
331cb0ef41Sopenharmony_ci    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
341cb0ef41Sopenharmony_ci  // Replace serialized references to the global proxy and its map with the
351cb0ef41Sopenharmony_ci  // given global proxy and its map.
361cb0ef41Sopenharmony_ci  AddAttachedObject(global_proxy);
371cb0ef41Sopenharmony_ci  AddAttachedObject(handle(global_proxy->map(), isolate));
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  Handle<Object> result;
401cb0ef41Sopenharmony_ci  {
411cb0ef41Sopenharmony_ci    // There's no code deserialized here. If this assert fires then that's
421cb0ef41Sopenharmony_ci    // changed and logging should be added to notify the profiler et al. of
431cb0ef41Sopenharmony_ci    // the new code, which also has to be flushed from instruction cache.
441cb0ef41Sopenharmony_ci    DisallowCodeAllocation no_code_allocation;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    result = ReadObject();
471cb0ef41Sopenharmony_ci    DeserializeDeferredObjects();
481cb0ef41Sopenharmony_ci    DeserializeEmbedderFields(embedder_fields_deserializer);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    LogNewMapEvents();
511cb0ef41Sopenharmony_ci    WeakenDescriptorArrays();
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  if (should_rehash()) Rehash();
551cb0ef41Sopenharmony_ci  SetupOffHeapArrayBufferBackingStores();
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  return result;
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_civoid ContextDeserializer::SetupOffHeapArrayBufferBackingStores() {
611cb0ef41Sopenharmony_ci  for (Handle<JSArrayBuffer> buffer : new_off_heap_array_buffers()) {
621cb0ef41Sopenharmony_ci    uint32_t store_index = buffer->GetBackingStoreRefForDeserialization();
631cb0ef41Sopenharmony_ci    auto bs = backing_store(store_index);
641cb0ef41Sopenharmony_ci    SharedFlag shared =
651cb0ef41Sopenharmony_ci        bs && bs->is_shared() ? SharedFlag::kShared : SharedFlag::kNotShared;
661cb0ef41Sopenharmony_ci    DCHECK_IMPLIES(bs, buffer->is_resizable() == bs->is_resizable());
671cb0ef41Sopenharmony_ci    ResizableFlag resizable = bs && bs->is_resizable()
681cb0ef41Sopenharmony_ci                                  ? ResizableFlag::kResizable
691cb0ef41Sopenharmony_ci                                  : ResizableFlag::kNotResizable;
701cb0ef41Sopenharmony_ci    buffer->Setup(shared, resizable, bs);
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_civoid ContextDeserializer::DeserializeEmbedderFields(
751cb0ef41Sopenharmony_ci    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
761cb0ef41Sopenharmony_ci  if (!source()->HasMore() || source()->Get() != kEmbedderFieldsData) return;
771cb0ef41Sopenharmony_ci  DisallowGarbageCollection no_gc;
781cb0ef41Sopenharmony_ci  DisallowJavascriptExecution no_js(isolate());
791cb0ef41Sopenharmony_ci  DisallowCompilation no_compile(isolate());
801cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(embedder_fields_deserializer.callback);
811cb0ef41Sopenharmony_ci  for (int code = source()->Get(); code != kSynchronize;
821cb0ef41Sopenharmony_ci       code = source()->Get()) {
831cb0ef41Sopenharmony_ci    HandleScope scope(isolate());
841cb0ef41Sopenharmony_ci    Handle<JSObject> obj = Handle<JSObject>::cast(GetBackReferencedObject());
851cb0ef41Sopenharmony_ci    int index = source()->GetInt();
861cb0ef41Sopenharmony_ci    int size = source()->GetInt();
871cb0ef41Sopenharmony_ci    // TODO(yangguo,jgruber): Turn this into a reusable shared buffer.
881cb0ef41Sopenharmony_ci    byte* data = new byte[size];
891cb0ef41Sopenharmony_ci    source()->CopyRaw(data, size);
901cb0ef41Sopenharmony_ci    embedder_fields_deserializer.callback(v8::Utils::ToLocal(obj), index,
911cb0ef41Sopenharmony_ci                                          {reinterpret_cast<char*>(data), size},
921cb0ef41Sopenharmony_ci                                          embedder_fields_deserializer.data);
931cb0ef41Sopenharmony_ci    delete[] data;
941cb0ef41Sopenharmony_ci  }
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_ci}  // namespace internal
971cb0ef41Sopenharmony_ci}  // namespace v8
98