this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "strarray-builtins.h"
3
4#include "builtins.h"
5#include "runtime.h"
6#include "type-builtins.h"
7
8namespace py {
9
10static const BuiltinAttribute kStrArrayAttributes[] = {
11 {ID(_str_array__items), RawStrArray::kItemsOffset, AttributeFlags::kHidden},
12 {ID(_str_array__num_items), RawStrArray::kNumItemsOffset,
13 AttributeFlags::kHidden},
14};
15
16void initializeStrArrayType(Thread* thread) {
17 addBuiltinType(thread, ID(_str_array), LayoutId::kStrArray,
18 /*superclass_id=*/LayoutId::kObject, kStrArrayAttributes,
19 StrArray::kSize, /*basetype=*/false);
20}
21
22RawObject METH(_str_array, __init__)(Thread* thread, Arguments args) {
23 HandleScope scope(thread);
24 Object self_obj(&scope, args.get(0));
25 if (!self_obj.isStrArray()) {
26 return thread->raiseRequiresType(self_obj, ID(_str_array));
27 }
28 StrArray self(&scope, *self_obj);
29 self.setNumItems(0);
30 Object source(&scope, args.get(1));
31 if (source.isUnbound()) {
32 return NoneType::object();
33 }
34 Runtime* runtime = thread->runtime();
35 if (!runtime->isInstanceOfStr(*source)) {
36 return thread->raiseWithFmt(LayoutId::kTypeError,
37 "_str_array can only be initialized with str");
38 }
39 Str source_str(&scope, strUnderlying(*source));
40 runtime->strArrayAddStr(thread, self, source_str);
41 return NoneType::object();
42}
43
44RawObject METH(_str_array, __new__)(Thread* thread, Arguments args) {
45 Runtime* runtime = thread->runtime();
46 DCHECK(args.get(0) == runtime->typeAt(LayoutId::kStrArray),
47 "_str_array.__new__(X): X is not '_str_array'");
48 return runtime->newStrArray();
49}
50
51RawObject METH(_str_array, __str__)(Thread* thread, Arguments args) {
52 HandleScope scope(thread);
53 Object self_obj(&scope, args.get(0));
54 if (!self_obj.isStrArray()) {
55 return thread->raiseRequiresType(self_obj, ID(_str_array));
56 }
57 StrArray self(&scope, *self_obj);
58 return thread->runtime()->strFromStrArray(self);
59}
60
61} // namespace py