• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use endian_scalar::read_scalar_at;
18 use follow::Follow;
19 use primitives::*;
20 
21 /// VTable encapsulates read-only usage of a vtable. It is only to be used
22 /// by generated code.
23 #[derive(Debug)]
24 pub struct VTable<'a> {
25     buf: &'a [u8],
26     loc: usize,
27 }
28 
29 impl<'a> PartialEq for VTable<'a> {
eq(&self, other: &VTable) -> bool30     fn eq(&self, other: &VTable) -> bool {
31         self.as_bytes().eq(other.as_bytes())
32     }
33 }
34 
35 impl<'a> VTable<'a> {
init(buf: &'a [u8], loc: usize) -> Self36     pub fn init(buf: &'a [u8], loc: usize) -> Self {
37         VTable {
38             buf: buf,
39             loc: loc,
40         }
41     }
num_fields(&self) -> usize42     pub fn num_fields(&self) -> usize {
43         (self.num_bytes() / SIZE_VOFFSET) - 2
44     }
num_bytes(&self) -> usize45     pub fn num_bytes(&self) -> usize {
46         read_scalar_at::<VOffsetT>(self.buf, self.loc) as usize
47     }
object_inline_num_bytes(&self) -> usize48     pub fn object_inline_num_bytes(&self) -> usize {
49         let n = read_scalar_at::<VOffsetT>(self.buf, self.loc + SIZE_VOFFSET);
50         n as usize
51     }
get_field(&self, idx: usize) -> VOffsetT52     pub fn get_field(&self, idx: usize) -> VOffsetT {
53         // TODO(rw): distinguish between None and 0?
54         if idx > self.num_fields() {
55             return 0;
56         }
57         read_scalar_at::<VOffsetT>(
58             self.buf,
59             self.loc + SIZE_VOFFSET + SIZE_VOFFSET + SIZE_VOFFSET * idx,
60         )
61     }
get(&self, byte_loc: VOffsetT) -> VOffsetT62     pub fn get(&self, byte_loc: VOffsetT) -> VOffsetT {
63         // TODO(rw): distinguish between None and 0?
64         if byte_loc as usize >= self.num_bytes() {
65             return 0;
66         }
67         read_scalar_at::<VOffsetT>(self.buf, self.loc + byte_loc as usize)
68     }
as_bytes(&self) -> &[u8]69     pub fn as_bytes(&self) -> &[u8] {
70         let len = self.num_bytes();
71         &self.buf[self.loc..self.loc + len]
72     }
73 }
74 
75 
76 #[allow(dead_code)]
field_index_to_field_offset(field_id: VOffsetT) -> VOffsetT77 pub fn field_index_to_field_offset(field_id: VOffsetT) -> VOffsetT {
78     // Should correspond to what end_table() below builds up.
79     let fixed_fields = 2; // Vtable size and Object Size.
80     ((field_id + fixed_fields) * (SIZE_VOFFSET as VOffsetT)) as VOffsetT
81 }
82 
83 #[allow(dead_code)]
field_offset_to_field_index(field_o: VOffsetT) -> VOffsetT84 pub fn field_offset_to_field_index(field_o: VOffsetT) -> VOffsetT {
85     debug_assert!(field_o >= 2);
86     let fixed_fields = 2; // VTable size and Object Size.
87     (field_o / (SIZE_VOFFSET as VOffsetT)) - fixed_fields
88 }
89 
90 impl<'a> Follow<'a> for VTable<'a> {
91     type Inner = VTable<'a>;
follow(buf: &'a [u8], loc: usize) -> Self::Inner92     fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
93         VTable::init(buf, loc)
94     }
95 }
96