Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / toolchain / eh_separate_files1.cc
1 /*
2  * Copyright (c) 2013 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include "native_client/tests/toolchain/eh_separate_files.h"
8
9 #include "native_client/tests/toolchain/eh_helper.h"
10
11 // Test that even with the non-stable exception handling PNaCl feature
12 // turned on, we generate the right calling conventions for
13 // portable (LE32) PNaCl bitcode.  Otherwise, this is like eh_catch_many.cc.
14
15 // Declare the functions from the file2 as if it was pass-by-value.
16 // However, the definition in file2 will actually be pass-by-pointer,
17 // because that is what PNaCl expands it into.
18
19 // We need extern "C" since the prototypes aren't going to match.
20 extern "C" {
21 extern __attribute__((noinline))
22 struct Var return_a_struct(int32_t set_fields_to);
23
24 extern __attribute__((noinline))
25 struct Var call_and_return(struct Var v);
26
27 extern __attribute__((noinline))
28 void call_with_struct(int32_t expected_values, struct Var v);
29
30 // file2 will call us back with a pointer, but we call it by-value.
31 extern __attribute__((noinline))
32 void call_with_callback(void (*fp)(struct Var* v), struct Var set_fields_to);
33 }  // extern "C"
34
35 class A {
36  public:
37   A() { next_step(4); }
38   ~A() { next_step(10); }
39 };
40
41
42 class B {
43  public:
44   B() { next_step(9); }
45   ~B() { next_step(12);}
46 };
47
48
49 class C {
50  public:
51   C() { abort(); }
52   ~C() { abort(); }
53 };
54
55 struct Var global_v;
56
57 void my_callback(struct Var* v) {
58   global_v = *v;
59 }
60
61 void inner() {
62   next_step(3);
63   A a;
64   try {
65     next_step(5);
66     int32_t kExpected = (1 << 20) - 1;
67     struct Var v = return_a_struct(kExpected);
68     if (v.type != kExpected
69         && v.value.as_double != (double)(kExpected)) {
70       printf("ERROR: return_a_struct mismatch: %d vs %d and %f vs %f\n",
71              v.type, kExpected, v.value.as_double, (double)kExpected);
72       abort();
73     }
74     next_step(6);
75     v = call_and_return(v);
76     next_step(7);
77     call_with_struct(kExpected, v);
78     // Also test callbacks.
79     call_with_callback(&my_callback, v);
80     if (global_v.type != kExpected
81         && global_v.value.as_double != (double)(kExpected)) {
82       printf("ERROR: callback mismatch: %d vs %d and %f vs %f\n",
83              global_v.type, kExpected,
84              global_v.value.as_double, (double)kExpected);
85       abort();
86     }
87     next_step(8);
88     throw B();
89     abort();
90   } catch(C &) {
91     abort();
92   }
93 }
94
95
96 int main() {
97   next_step(1);
98
99   try {
100     next_step(2);
101     inner();
102   } catch(...) {
103     next_step(11);
104   }
105   next_step(13);
106
107   return 55;
108 }